ds.c revision 193149
1/* 2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (C) 2002, 2003 Internet Software Consortium. 4 * 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. 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.11 2007/06/19 23:47:16 tbox Exp $ */ 19 20/*! \file */ 21 22#include <config.h> 23 24#include <string.h> 25 26#include <isc/buffer.h> 27#include <isc/region.h> 28#include <isc/sha1.h> 29#include <isc/sha2.h> 30#include <isc/util.h> 31 32#include <dns/ds.h> 33#include <dns/fixedname.h> 34#include <dns/name.h> 35#include <dns/rdata.h> 36#include <dns/rdatastruct.h> 37#include <dns/result.h> 38 39#include <dst/dst.h> 40 41isc_result_t 42dns_ds_buildrdata(dns_name_t *owner, dns_rdata_t *key, 43 unsigned int digest_type, unsigned char *buffer, 44 dns_rdata_t *rdata) 45{ 46 dns_fixedname_t fname; 47 dns_name_t *name; 48 unsigned char digest[ISC_SHA256_DIGESTLENGTH]; 49 isc_region_t r; 50 isc_buffer_t b; 51 dns_rdata_ds_t ds; 52 53 REQUIRE(key != NULL); 54 REQUIRE(key->type == dns_rdatatype_dnskey); 55 56 if (!dns_ds_digest_supported(digest_type)) 57 return (ISC_R_NOTIMPLEMENTED); 58 59 dns_fixedname_init(&fname); 60 name = dns_fixedname_name(&fname); 61 (void)dns_name_downcase(owner, name, NULL); 62 63 memset(buffer, 0, DNS_DS_BUFFERSIZE); 64 isc_buffer_init(&b, buffer, DNS_DS_BUFFERSIZE); 65 66 if (digest_type == DNS_DSDIGEST_SHA1) { 67 isc_sha1_t sha1; 68 isc_sha1_init(&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); 75 } else { 76 isc_sha256_t sha256; 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); 84 } 85 86 ds.mctx = NULL; 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; 94 ds.digest = digest; 95 96 return (dns_rdata_fromstruct(rdata, key->rdclass, dns_rdatatype_ds, 97 &ds, &b)); 98} 99 100isc_boolean_t 101dns_ds_digest_supported(unsigned int digest_type) { 102 return (ISC_TF(digest_type == DNS_DSDIGEST_SHA1 || 103 digest_type == DNS_DSDIGEST_SHA256)); 104} 105