2002-06-17 04:01:37 +00:00
|
|
|
/*
|
2012-05-02 23:45:44 +00:00
|
|
|
* Copyright (C) 2004-2007, 2010, 2012 Internet Systems Consortium, Inc. ("ISC")
|
2004-03-05 05:14:21 +00:00
|
|
|
* Copyright (C) 2002, 2003 Internet Software Consortium.
|
2002-06-17 04:01:37 +00:00
|
|
|
*
|
2007-06-18 23:47:57 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2002-06-17 04:01:37 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
2002-06-17 04:01:37 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-23 23:47:08 +00:00
|
|
|
/* $Id: ds.c,v 1.13 2010/12/23 23:47:08 tbox Exp $ */
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2002-06-17 04:01:37 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/region.h>
|
|
|
|
#include <isc/sha1.h>
|
2006-02-21 23:49:51 +00:00
|
|
|
#include <isc/sha2.h>
|
2002-06-17 04:01:37 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/ds.h>
|
|
|
|
#include <dns/fixedname.h>
|
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/rdata.h>
|
|
|
|
#include <dns/rdatastruct.h>
|
|
|
|
#include <dns/result.h>
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
|
|
|
|
2014-01-14 15:40:56 -08:00
|
|
|
#if defined(HAVE_OPENSSL_GOST) || defined(HAVE_PKCS11_GOST)
|
|
|
|
#include "dst_gost.h"
|
2010-12-23 04:08:00 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-17 04:01:37 +00:00
|
|
|
isc_result_t
|
|
|
|
dns_ds_buildrdata(dns_name_t *owner, dns_rdata_t *key,
|
|
|
|
unsigned int digest_type, unsigned char *buffer,
|
|
|
|
dns_rdata_t *rdata)
|
|
|
|
{
|
|
|
|
dns_fixedname_t fname;
|
|
|
|
dns_name_t *name;
|
2012-05-02 23:20:17 +10:00
|
|
|
unsigned char digest[ISC_SHA384_DIGESTLENGTH];
|
2002-06-17 04:01:37 +00:00
|
|
|
isc_region_t r;
|
|
|
|
isc_buffer_t b;
|
|
|
|
dns_rdata_ds_t ds;
|
2010-12-23 04:08:00 +00:00
|
|
|
isc_sha1_t sha1;
|
|
|
|
isc_sha256_t sha256;
|
2012-05-02 23:20:17 +10:00
|
|
|
isc_sha384_t sha384;
|
2014-01-14 15:40:56 -08:00
|
|
|
#if defined(HAVE_OPENSSL_GOST) || defined(HAVE_PKCS11_GOST)
|
|
|
|
isc_gost_t gost;
|
2010-12-23 04:08:00 +00:00
|
|
|
#endif
|
2002-06-17 04:01:37 +00:00
|
|
|
|
2003-09-30 06:00:40 +00:00
|
|
|
REQUIRE(key != NULL);
|
|
|
|
REQUIRE(key->type == dns_rdatatype_dnskey);
|
|
|
|
|
2012-10-03 12:38:43 +10:00
|
|
|
if (!dst_ds_digest_supported(digest_type))
|
2002-06-17 04:01:37 +00:00
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
|
|
|
|
dns_fixedname_init(&fname);
|
|
|
|
name = dns_fixedname_name(&fname);
|
|
|
|
(void)dns_name_downcase(owner, name, NULL);
|
|
|
|
|
|
|
|
memset(buffer, 0, DNS_DS_BUFFERSIZE);
|
|
|
|
isc_buffer_init(&b, buffer, DNS_DS_BUFFERSIZE);
|
|
|
|
|
2010-12-23 04:08:00 +00:00
|
|
|
switch (digest_type) {
|
|
|
|
case DNS_DSDIGEST_SHA1:
|
2006-02-21 23:49:51 +00:00
|
|
|
isc_sha1_init(&sha1);
|
|
|
|
dns_name_toregion(name, &r);
|
|
|
|
isc_sha1_update(&sha1, r.base, r.length);
|
|
|
|
dns_rdata_toregion(key, &r);
|
|
|
|
INSIST(r.length >= 4);
|
|
|
|
isc_sha1_update(&sha1, r.base, r.length);
|
|
|
|
isc_sha1_final(&sha1, digest);
|
2010-12-23 04:08:00 +00:00
|
|
|
break;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2014-01-14 15:40:56 -08:00
|
|
|
#if defined(HAVE_OPENSSL_GOST) || defined(HAVE_PKCS11_GOST)
|
|
|
|
#define RETERR(x) do { \
|
|
|
|
isc_result_t ret = (x); \
|
|
|
|
if (ret != ISC_R_SUCCESS) { \
|
|
|
|
isc_gost_invalidate(&gost); \
|
|
|
|
return (ret); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2010-12-23 04:08:00 +00:00
|
|
|
|
|
|
|
case DNS_DSDIGEST_GOST:
|
2014-01-14 15:40:56 -08:00
|
|
|
RETERR(isc_gost_init(&gost));
|
2010-12-23 04:08:00 +00:00
|
|
|
dns_name_toregion(name, &r);
|
2014-01-14 15:40:56 -08:00
|
|
|
RETERR(isc_gost_update(&gost, r.base, r.length));
|
2010-12-23 04:08:00 +00:00
|
|
|
dns_rdata_toregion(key, &r);
|
|
|
|
INSIST(r.length >= 4);
|
2014-01-14 15:40:56 -08:00
|
|
|
RETERR(isc_gost_update(&gost, r.base, r.length));
|
|
|
|
RETERR(isc_gost_final(&gost, digest));
|
2010-12-23 04:08:00 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
case DNS_DSDIGEST_SHA384:
|
|
|
|
isc_sha384_init(&sha384);
|
|
|
|
dns_name_toregion(name, &r);
|
|
|
|
isc_sha384_update(&sha384, r.base, r.length);
|
|
|
|
dns_rdata_toregion(key, &r);
|
|
|
|
INSIST(r.length >= 4);
|
|
|
|
isc_sha384_update(&sha384, r.base, r.length);
|
|
|
|
isc_sha384_final(digest, &sha384);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DNS_DSDIGEST_SHA256:
|
2010-12-23 04:08:00 +00:00
|
|
|
default:
|
2006-02-21 23:49:51 +00:00
|
|
|
isc_sha256_init(&sha256);
|
|
|
|
dns_name_toregion(name, &r);
|
|
|
|
isc_sha256_update(&sha256, r.base, r.length);
|
|
|
|
dns_rdata_toregion(key, &r);
|
|
|
|
INSIST(r.length >= 4);
|
|
|
|
isc_sha256_update(&sha256, r.base, r.length);
|
|
|
|
isc_sha256_final(digest, &sha256);
|
2010-12-23 04:08:00 +00:00
|
|
|
break;
|
2006-02-21 23:49:51 +00:00
|
|
|
}
|
2002-06-17 04:01:37 +00:00
|
|
|
|
|
|
|
ds.mctx = NULL;
|
|
|
|
ds.common.rdclass = key->rdclass;
|
|
|
|
ds.common.rdtype = dns_rdatatype_ds;
|
|
|
|
ds.algorithm = r.base[3];
|
|
|
|
ds.key_tag = dst_region_computeid(&r, ds.algorithm);
|
2006-02-21 23:49:51 +00:00
|
|
|
ds.digest_type = digest_type;
|
2010-12-23 04:08:00 +00:00
|
|
|
switch (digest_type) {
|
|
|
|
case DNS_DSDIGEST_SHA1:
|
|
|
|
ds.length = ISC_SHA1_DIGESTLENGTH;
|
|
|
|
break;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2014-01-14 15:40:56 -08:00
|
|
|
#if defined(HAVE_OPENSSL_GOST) || defined(HAVE_PKCS11_GOST)
|
2010-12-23 04:08:00 +00:00
|
|
|
case DNS_DSDIGEST_GOST:
|
|
|
|
ds.length = ISC_GOST_DIGESTLENGTH;
|
|
|
|
break;
|
|
|
|
#endif
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
case DNS_DSDIGEST_SHA384:
|
|
|
|
ds.length = ISC_SHA384_DIGESTLENGTH;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DNS_DSDIGEST_SHA256:
|
2010-12-23 04:08:00 +00:00
|
|
|
default:
|
|
|
|
ds.length = ISC_SHA256_DIGESTLENGTH;
|
|
|
|
break;
|
|
|
|
}
|
2002-06-17 04:01:37 +00:00
|
|
|
ds.digest = digest;
|
|
|
|
|
|
|
|
return (dns_rdata_fromstruct(rdata, key->rdclass, dns_rdatatype_ds,
|
|
|
|
&ds, &b));
|
|
|
|
}
|