Home | History | Annotate | Line # | Download | only in dns
      1 /*	$NetBSD: keydata.c,v 1.7 2025/01/26 16:25:23 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 <inttypes.h>
     19 
     20 #include <isc/buffer.h>
     21 #include <isc/mem.h>
     22 #include <isc/string.h>
     23 #include <isc/util.h>
     24 
     25 #include <dns/keydata.h>
     26 #include <dns/rdata.h>
     27 #include <dns/rdatastruct.h>
     28 
     29 isc_result_t
     30 dns_keydata_todnskey(dns_rdata_keydata_t *keydata, dns_rdata_dnskey_t *dnskey,
     31 		     isc_mem_t *mctx) {
     32 	REQUIRE(keydata != NULL && dnskey != NULL);
     33 
     34 	dnskey->common.rdtype = dns_rdatatype_dnskey;
     35 	dnskey->common.rdclass = keydata->common.rdclass;
     36 	dnskey->mctx = mctx;
     37 	dnskey->flags = keydata->flags;
     38 	dnskey->protocol = keydata->protocol;
     39 	dnskey->algorithm = keydata->algorithm;
     40 
     41 	dnskey->datalen = keydata->datalen;
     42 
     43 	if (mctx == NULL) {
     44 		dnskey->data = keydata->data;
     45 	} else {
     46 		dnskey->data = isc_mem_allocate(mctx, dnskey->datalen);
     47 		memmove(dnskey->data, keydata->data, dnskey->datalen);
     48 	}
     49 
     50 	return ISC_R_SUCCESS;
     51 }
     52 
     53 isc_result_t
     54 dns_keydata_fromdnskey(dns_rdata_keydata_t *keydata, dns_rdata_dnskey_t *dnskey,
     55 		       uint32_t refresh, uint32_t addhd, uint32_t removehd,
     56 		       isc_mem_t *mctx) {
     57 	REQUIRE(keydata != NULL && dnskey != NULL);
     58 
     59 	keydata->common.rdtype = dns_rdatatype_keydata;
     60 	keydata->common.rdclass = dnskey->common.rdclass;
     61 	keydata->mctx = mctx;
     62 	keydata->refresh = refresh;
     63 	keydata->addhd = addhd;
     64 	keydata->removehd = removehd;
     65 	keydata->flags = dnskey->flags;
     66 	keydata->protocol = dnskey->protocol;
     67 	keydata->algorithm = dnskey->algorithm;
     68 
     69 	keydata->datalen = dnskey->datalen;
     70 	if (mctx == NULL) {
     71 		keydata->data = dnskey->data;
     72 	} else {
     73 		keydata->data = isc_mem_allocate(mctx, keydata->datalen);
     74 		memmove(keydata->data, dnskey->data, keydata->datalen);
     75 	}
     76 
     77 	return ISC_R_SUCCESS;
     78 }
     79