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