ds.c revision 0b09763c354ec91fb352b6b4cea383bd0195b2d8
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews/*
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews *
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * Permission to use, copy, modify, and distribute this software for any
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * purpose with or without fee is hereby granted, provided that the above
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * copyright notice and this permission notice appear in all copies.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews *
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews */
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews/* $Id: ds.c,v 1.2 2002/06/17 04:01:18 marka Exp $ */
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <config.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <string.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/buffer.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/region.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/sha1.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <isc/util.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <dns/ds.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <dns/fixedname.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <dns/name.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <dns/rdata.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <dns/rdatastruct.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <dns/result.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews#include <dst/dst.h>
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsisc_result_t
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrewsdns_ds_buildrdata(dns_name_t *owner, dns_rdata_t *key,
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews unsigned int digest_type, unsigned char *buffer,
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews dns_rdata_t *rdata)
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews{
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_sha1_t sha1;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews dns_fixedname_t fname;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews dns_name_t *name;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews unsigned char digest[ISC_SHA1_DIGESTLENGTH];
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_region_t r;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_buffer_t b;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews dns_rdata_ds_t ds;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews if (digest_type != DNS_DSDIGEST_SHA1)
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews return (ISC_R_NOTIMPLEMENTED);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews dns_fixedname_init(&fname);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews name = dns_fixedname_name(&fname);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews (void)dns_name_downcase(owner, name, NULL);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews memset(buffer, 0, DNS_DS_BUFFERSIZE);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_buffer_init(&b, buffer, DNS_DS_BUFFERSIZE);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_sha1_init(&sha1);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews dns_name_toregion(name, &r);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_sha1_update(&sha1, r.base, r.length);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews dns_rdata_toregion(key, &r);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews INSIST(r.length >= 4);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_sha1_update(&sha1, r.base, r.length);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews isc_sha1_final(&sha1, digest);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.mctx = NULL;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.common.rdclass = key->rdclass;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.common.rdtype = dns_rdatatype_ds;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.algorithm = r.base[3];
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.key_tag = dst_region_computeid(&r, ds.algorithm);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.digest_type = DNS_DSDIGEST_SHA1;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.length = ISC_SHA1_DIGESTLENGTH;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews ds.digest = digest;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews return (dns_rdata_fromstruct(rdata, key->rdclass, dns_rdatatype_ds,
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews &ds, &b));
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews}