Home | History | Annotate | Line # | Download | only in dns
keydata.c revision 1.2.2.3
      1 /*	$NetBSD: keydata.c,v 1.2.2.3 2019/01/18 08:49:53 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * This Source Code Form is subject to the terms of the Mozilla Public
      7  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  *
     10  * See the COPYRIGHT file distributed with this work for additional
     11  * information regarding copyright ownership.
     12  */
     13 
     14 
     15 /*! \file */
     16 
     17 #include <config.h>
     18 
     19 #include <inttypes.h>
     20 
     21 #include <isc/buffer.h>
     22 #include <isc/mem.h>
     23 #include <isc/string.h>
     24 #include <isc/util.h>
     25 
     26 #include <dns/rdata.h>
     27 #include <dns/rdatastruct.h>
     28 #include <dns/keydata.h>
     29 
     30 isc_result_t
     31 dns_keydata_todnskey(dns_rdata_keydata_t *keydata,
     32 		     dns_rdata_dnskey_t *dnskey, isc_mem_t *mctx)
     33 {
     34 	REQUIRE(keydata != NULL && dnskey != NULL);
     35 
     36 	dnskey->common.rdtype = dns_rdatatype_dnskey;
     37 	dnskey->common.rdclass = keydata->common.rdclass;
     38 	dnskey->mctx = mctx;
     39 	dnskey->flags = keydata->flags;
     40 	dnskey->protocol = keydata->protocol;
     41 	dnskey->algorithm = keydata->algorithm;
     42 
     43 	dnskey->datalen = keydata->datalen;
     44 
     45 	if (mctx == NULL)
     46 		dnskey->data = keydata->data;
     47 	else {
     48 		dnskey->data = isc_mem_allocate(mctx, dnskey->datalen);
     49 		if (dnskey->data == NULL)
     50 			return (ISC_R_NOMEMORY);
     51 		memmove(dnskey->data, keydata->data, dnskey->datalen);
     52 	}
     53 
     54 	return (ISC_R_SUCCESS);
     55 }
     56 
     57 isc_result_t
     58 dns_keydata_fromdnskey(dns_rdata_keydata_t *keydata,
     59 		       dns_rdata_dnskey_t *dnskey,
     60 		       uint32_t refresh, uint32_t addhd,
     61 		       uint32_t removehd, isc_mem_t *mctx)
     62 {
     63 	REQUIRE(keydata != NULL && dnskey != NULL);
     64 
     65 	keydata->common.rdtype = dns_rdatatype_keydata;
     66 	keydata->common.rdclass = dnskey->common.rdclass;
     67 	keydata->mctx = mctx;
     68 	keydata->refresh = refresh;
     69 	keydata->addhd = addhd;
     70 	keydata->removehd = removehd;
     71 	keydata->flags = dnskey->flags;
     72 	keydata->protocol = dnskey->protocol;
     73 	keydata->algorithm = dnskey->algorithm;
     74 
     75 	keydata->datalen = dnskey->datalen;
     76 	if (mctx == NULL)
     77 		keydata->data = dnskey->data;
     78 	else {
     79 		keydata->data = isc_mem_allocate(mctx, keydata->datalen);
     80 		if (keydata->data == NULL)
     81 			return (ISC_R_NOMEMORY);
     82 		memmove(keydata->data, dnskey->data, keydata->datalen);
     83 	}
     84 
     85 	return (ISC_R_SUCCESS);
     86 }
     87