Merge from vendor branch FILE:
[dragonfly.git] / contrib / bind-9.3 / lib / dns / ds.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2002, 2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: ds.c,v 1.4.2.1 2004/03/08 02:07:53 marka Exp $ */
19
20 #include <config.h>
21
22 #include <string.h>
23
24 #include <isc/buffer.h>
25 #include <isc/region.h>
26 #include <isc/sha1.h>
27 #include <isc/util.h>
28
29 #include <dns/ds.h>
30 #include <dns/fixedname.h>
31 #include <dns/name.h>
32 #include <dns/rdata.h>
33 #include <dns/rdatastruct.h>
34 #include <dns/result.h>
35
36 #include <dst/dst.h>
37
38 isc_result_t
39 dns_ds_buildrdata(dns_name_t *owner, dns_rdata_t *key,
40                   unsigned int digest_type, unsigned char *buffer,
41                   dns_rdata_t *rdata)
42 {
43         isc_sha1_t sha1;
44         dns_fixedname_t fname;
45         dns_name_t *name;
46         unsigned char digest[ISC_SHA1_DIGESTLENGTH];
47         isc_region_t r;
48         isc_buffer_t b;
49         dns_rdata_ds_t ds;
50
51         REQUIRE(key != NULL);
52         REQUIRE(key->type == dns_rdatatype_dnskey);
53
54         if (digest_type != DNS_DSDIGEST_SHA1)
55                 return (ISC_R_NOTIMPLEMENTED);
56
57         dns_fixedname_init(&fname);
58         name = dns_fixedname_name(&fname);
59         (void)dns_name_downcase(owner, name, NULL);
60
61         memset(buffer, 0, DNS_DS_BUFFERSIZE);
62         isc_buffer_init(&b, buffer, DNS_DS_BUFFERSIZE);
63
64         isc_sha1_init(&sha1);
65         dns_name_toregion(name, &r);
66         isc_sha1_update(&sha1, r.base, r.length);
67         dns_rdata_toregion(key, &r);
68         INSIST(r.length >= 4);
69         isc_sha1_update(&sha1, r.base, r.length);
70         isc_sha1_final(&sha1, digest);
71
72         ds.mctx = NULL;
73         ds.common.rdclass = key->rdclass;
74         ds.common.rdtype = dns_rdatatype_ds;
75         ds.algorithm = r.base[3];
76         ds.key_tag = dst_region_computeid(&r, ds.algorithm);
77         ds.digest_type = DNS_DSDIGEST_SHA1;
78         ds.length = ISC_SHA1_DIGESTLENGTH;
79         ds.digest = digest;
80
81         return (dns_rdata_fromstruct(rdata, key->rdclass, dns_rdatatype_ds,
82                                      &ds, &b));
83 }