1 /* $NetBSD: ds.c,v 1.10 2025/01/26 16:25:22 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 /*! \file */ 17 18 #include <string.h> 19 20 #include <isc/buffer.h> 21 #include <isc/md.h> 22 #include <isc/region.h> 23 #include <isc/result.h> 24 #include <isc/util.h> 25 26 #include <dns/ds.h> 27 #include <dns/fixedname.h> 28 #include <dns/name.h> 29 #include <dns/rdata.h> 30 #include <dns/rdatastruct.h> 31 32 #include <dst/dst.h> 33 34 isc_result_t 35 dns_ds_fromkeyrdata(const dns_name_t *owner, dns_rdata_t *key, 36 dns_dsdigest_t digest_type, unsigned char *digest, 37 dns_rdata_ds_t *dsrdata) { 38 isc_result_t result; 39 dns_fixedname_t fname; 40 dns_name_t *name; 41 unsigned int digestlen; 42 isc_region_t r; 43 isc_md_t *md; 44 const isc_md_type_t *md_type = NULL; 45 46 REQUIRE(key != NULL); 47 REQUIRE(key->type == dns_rdatatype_dnskey || 48 key->type == dns_rdatatype_cdnskey); 49 50 if (!dst_ds_digest_supported(digest_type)) { 51 return ISC_R_NOTIMPLEMENTED; 52 } 53 54 switch (digest_type) { 55 case DNS_DSDIGEST_SHA1: 56 md_type = ISC_MD_SHA1; 57 break; 58 59 case DNS_DSDIGEST_SHA384: 60 md_type = ISC_MD_SHA384; 61 break; 62 63 case DNS_DSDIGEST_SHA256: 64 md_type = ISC_MD_SHA256; 65 break; 66 67 default: 68 UNREACHABLE(); 69 } 70 71 name = dns_fixedname_initname(&fname); 72 (void)dns_name_downcase(owner, name, NULL); 73 74 md = isc_md_new(); 75 if (md == NULL) { 76 return ISC_R_NOMEMORY; 77 } 78 79 result = isc_md_init(md, md_type); 80 if (result != ISC_R_SUCCESS) { 81 goto end; 82 } 83 84 dns_name_toregion(name, &r); 85 86 result = isc_md_update(md, r.base, r.length); 87 if (result != ISC_R_SUCCESS) { 88 goto end; 89 } 90 91 dns_rdata_toregion(key, &r); 92 INSIST(r.length >= 4); 93 94 result = isc_md_update(md, r.base, r.length); 95 if (result != ISC_R_SUCCESS) { 96 goto end; 97 } 98 99 result = isc_md_final(md, digest, &digestlen); 100 if (result != ISC_R_SUCCESS) { 101 goto end; 102 } 103 104 dsrdata->mctx = NULL; 105 dsrdata->common.rdclass = key->rdclass; 106 dsrdata->common.rdtype = dns_rdatatype_ds; 107 dsrdata->algorithm = r.base[3]; 108 dsrdata->key_tag = dst_region_computeid(&r); 109 dsrdata->digest_type = digest_type; 110 dsrdata->digest = digest; 111 dsrdata->length = digestlen; 112 113 end: 114 isc_md_free(md); 115 return result; 116 } 117 118 isc_result_t 119 dns_ds_buildrdata(dns_name_t *owner, dns_rdata_t *key, 120 dns_dsdigest_t digest_type, unsigned char *buffer, 121 dns_rdata_t *rdata) { 122 isc_result_t result; 123 unsigned char digest[ISC_MAX_MD_SIZE]; 124 dns_rdata_ds_t ds; 125 isc_buffer_t b; 126 127 result = dns_ds_fromkeyrdata(owner, key, digest_type, digest, &ds); 128 if (result != ISC_R_SUCCESS) { 129 return result; 130 } 131 132 memset(buffer, 0, DNS_DS_BUFFERSIZE); 133 isc_buffer_init(&b, buffer, DNS_DS_BUFFERSIZE); 134 result = dns_rdata_fromstruct(rdata, key->rdclass, dns_rdatatype_ds, 135 &ds, &b); 136 return result; 137 } 138