Home | History | Annotate | Line # | Download | only in dns
dst_api.c revision 1.1
      1  1.1  christos /*	$NetBSD: dst_api.c,v 1.1 2018/08/12 12:08:14 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.1  christos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     11  1.1  christos  * information regarding copyright ownership.
     12  1.1  christos  *
     13  1.1  christos  * Portions Copyright (C) Network Associates, Inc.
     14  1.1  christos  *
     15  1.1  christos  * Permission to use, copy, modify, and/or distribute this software for any
     16  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     17  1.1  christos  * copyright notice and this permission notice appear in all copies.
     18  1.1  christos  *
     19  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
     20  1.1  christos  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
     21  1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
     22  1.1  christos  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     23  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     24  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
     25  1.1  christos  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     26  1.1  christos  */
     27  1.1  christos 
     28  1.1  christos /*! \file */
     29  1.1  christos 
     30  1.1  christos #include <config.h>
     31  1.1  christos 
     32  1.1  christos #include <stdlib.h>
     33  1.1  christos #include <time.h>
     34  1.1  christos 
     35  1.1  christos #include <isc/buffer.h>
     36  1.1  christos #include <isc/dir.h>
     37  1.1  christos #include <isc/entropy.h>
     38  1.1  christos #include <isc/fsaccess.h>
     39  1.1  christos #include <isc/hmacsha.h>
     40  1.1  christos #include <isc/lex.h>
     41  1.1  christos #include <isc/mem.h>
     42  1.1  christos #include <isc/once.h>
     43  1.1  christos #include <isc/platform.h>
     44  1.1  christos #include <isc/print.h>
     45  1.1  christos #include <isc/refcount.h>
     46  1.1  christos #include <isc/random.h>
     47  1.1  christos #include <isc/safe.h>
     48  1.1  christos #include <isc/string.h>
     49  1.1  christos #include <isc/time.h>
     50  1.1  christos #include <isc/util.h>
     51  1.1  christos #include <isc/file.h>
     52  1.1  christos 
     53  1.1  christos #include <pk11/site.h>
     54  1.1  christos 
     55  1.1  christos #define DST_KEY_INTERNAL
     56  1.1  christos 
     57  1.1  christos #include <dns/fixedname.h>
     58  1.1  christos #include <dns/keyvalues.h>
     59  1.1  christos #include <dns/name.h>
     60  1.1  christos #include <dns/rdata.h>
     61  1.1  christos #include <dns/rdataclass.h>
     62  1.1  christos #include <dns/ttl.h>
     63  1.1  christos #include <dns/types.h>
     64  1.1  christos 
     65  1.1  christos #include <dst/result.h>
     66  1.1  christos 
     67  1.1  christos #include "dst_internal.h"
     68  1.1  christos 
     69  1.1  christos #define DST_AS_STR(t) ((t).value.as_textregion.base)
     70  1.1  christos 
     71  1.1  christos static dst_func_t *dst_t_func[DST_MAX_ALGS];
     72  1.1  christos static isc_entropy_t *dst_entropy_pool = NULL;
     73  1.1  christos static unsigned int dst_entropy_flags = 0;
     74  1.1  christos 
     75  1.1  christos static isc_boolean_t dst_initialized = ISC_FALSE;
     76  1.1  christos 
     77  1.1  christos void gss_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
     78  1.1  christos 
     79  1.1  christos LIBDNS_EXTERNAL_DATA isc_mem_t *dst__memory_pool = NULL;
     80  1.1  christos 
     81  1.1  christos /*
     82  1.1  christos  * Static functions.
     83  1.1  christos  */
     84  1.1  christos static dst_key_t *	get_key_struct(const dns_name_t *name,
     85  1.1  christos 				       unsigned int alg,
     86  1.1  christos 				       unsigned int flags,
     87  1.1  christos 				       unsigned int protocol,
     88  1.1  christos 				       unsigned int bits,
     89  1.1  christos 				       dns_rdataclass_t rdclass,
     90  1.1  christos 				       dns_ttl_t ttl,
     91  1.1  christos 				       isc_mem_t *mctx);
     92  1.1  christos static isc_result_t	write_public_key(const dst_key_t *key, int type,
     93  1.1  christos 					 const char *directory);
     94  1.1  christos static isc_result_t	buildfilename(dns_name_t *name,
     95  1.1  christos 				      dns_keytag_t id,
     96  1.1  christos 				      unsigned int alg,
     97  1.1  christos 				      unsigned int type,
     98  1.1  christos 				      const char *directory,
     99  1.1  christos 				      isc_buffer_t *out);
    100  1.1  christos static isc_result_t	computeid(dst_key_t *key);
    101  1.1  christos static isc_result_t	frombuffer(const dns_name_t *name,
    102  1.1  christos 				   unsigned int alg,
    103  1.1  christos 				   unsigned int flags,
    104  1.1  christos 				   unsigned int protocol,
    105  1.1  christos 				   dns_rdataclass_t rdclass,
    106  1.1  christos 				   isc_buffer_t *source,
    107  1.1  christos 				   isc_mem_t *mctx,
    108  1.1  christos 				   dst_key_t **keyp);
    109  1.1  christos 
    110  1.1  christos static isc_result_t	algorithm_status(unsigned int alg);
    111  1.1  christos 
    112  1.1  christos static isc_result_t	addsuffix(char *filename, int len,
    113  1.1  christos 				  const char *dirname, const char *ofilename,
    114  1.1  christos 				  const char *suffix);
    115  1.1  christos 
    116  1.1  christos #define RETERR(x)				\
    117  1.1  christos 	do {					\
    118  1.1  christos 		result = (x);			\
    119  1.1  christos 		if (result != ISC_R_SUCCESS)	\
    120  1.1  christos 			goto out;		\
    121  1.1  christos 	} while (0)
    122  1.1  christos 
    123  1.1  christos #define CHECKALG(alg)				\
    124  1.1  christos 	do {					\
    125  1.1  christos 		isc_result_t _r;		\
    126  1.1  christos 		_r = algorithm_status(alg);	\
    127  1.1  christos 		if (_r != ISC_R_SUCCESS)	\
    128  1.1  christos 			return (_r);		\
    129  1.1  christos 	} while (0);				\
    130  1.1  christos 
    131  1.1  christos #if defined(OPENSSL)
    132  1.1  christos static void *
    133  1.1  christos default_memalloc(void *arg, size_t size) {
    134  1.1  christos 	UNUSED(arg);
    135  1.1  christos 	if (size == 0U)
    136  1.1  christos 		size = 1;
    137  1.1  christos 	return (malloc(size));
    138  1.1  christos }
    139  1.1  christos 
    140  1.1  christos static void
    141  1.1  christos default_memfree(void *arg, void *ptr) {
    142  1.1  christos 	UNUSED(arg);
    143  1.1  christos 	free(ptr);
    144  1.1  christos }
    145  1.1  christos #endif
    146  1.1  christos 
    147  1.1  christos isc_result_t
    148  1.1  christos dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags) {
    149  1.1  christos 	return (dst_lib_init2(mctx, ectx, NULL, eflags));
    150  1.1  christos }
    151  1.1  christos 
    152  1.1  christos isc_result_t
    153  1.1  christos dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
    154  1.1  christos 	      const char *engine, unsigned int eflags) {
    155  1.1  christos 	isc_result_t result;
    156  1.1  christos 
    157  1.1  christos 	REQUIRE(mctx != NULL);
    158  1.1  christos 	REQUIRE(dst_initialized == ISC_FALSE);
    159  1.1  christos 
    160  1.1  christos #if !defined(OPENSSL) && !defined(PKCS11CRYPTO)
    161  1.1  christos 	UNUSED(engine);
    162  1.1  christos #endif
    163  1.1  christos 
    164  1.1  christos 	dst__memory_pool = NULL;
    165  1.1  christos 
    166  1.1  christos #if defined(OPENSSL)
    167  1.1  christos 	UNUSED(mctx);
    168  1.1  christos 	/*
    169  1.1  christos 	 * When using --with-openssl, there seems to be no good way of not
    170  1.1  christos 	 * leaking memory due to the openssl error handling mechanism.
    171  1.1  christos 	 * Avoid assertions by using a local memory context and not checking
    172  1.1  christos 	 * for leaks on exit.  Note: as there are leaks we cannot use
    173  1.1  christos 	 * ISC_MEMFLAG_INTERNAL as it will free up memory still being used
    174  1.1  christos 	 * by libcrypto.
    175  1.1  christos 	 */
    176  1.1  christos 	result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
    177  1.1  christos 				  NULL, &dst__memory_pool, 0);
    178  1.1  christos 	if (result != ISC_R_SUCCESS)
    179  1.1  christos 		return (result);
    180  1.1  christos 	isc_mem_setname(dst__memory_pool, "dst", NULL);
    181  1.1  christos #ifndef OPENSSL_LEAKS
    182  1.1  christos 	isc_mem_setdestroycheck(dst__memory_pool, ISC_FALSE);
    183  1.1  christos #endif
    184  1.1  christos #else /* OPENSSL */
    185  1.1  christos 	isc_mem_attach(mctx, &dst__memory_pool);
    186  1.1  christos #endif /* OPENSSL */
    187  1.1  christos 	if (ectx != NULL) {
    188  1.1  christos 		isc_entropy_attach(ectx, &dst_entropy_pool);
    189  1.1  christos 		dst_entropy_flags = eflags;
    190  1.1  christos 	}
    191  1.1  christos 
    192  1.1  christos 	dst_result_register();
    193  1.1  christos 
    194  1.1  christos 	memset(dst_t_func, 0, sizeof(dst_t_func));
    195  1.1  christos #ifndef PK11_MD5_DISABLE
    196  1.1  christos 	RETERR(dst__hmacmd5_init(&dst_t_func[DST_ALG_HMACMD5]));
    197  1.1  christos #endif
    198  1.1  christos 	RETERR(dst__hmacsha1_init(&dst_t_func[DST_ALG_HMACSHA1]));
    199  1.1  christos 	RETERR(dst__hmacsha224_init(&dst_t_func[DST_ALG_HMACSHA224]));
    200  1.1  christos 	RETERR(dst__hmacsha256_init(&dst_t_func[DST_ALG_HMACSHA256]));
    201  1.1  christos 	RETERR(dst__hmacsha384_init(&dst_t_func[DST_ALG_HMACSHA384]));
    202  1.1  christos 	RETERR(dst__hmacsha512_init(&dst_t_func[DST_ALG_HMACSHA512]));
    203  1.1  christos #ifdef OPENSSL
    204  1.1  christos 	RETERR(dst__openssl_init(engine));
    205  1.1  christos #ifndef PK11_MD5_DISABLE
    206  1.1  christos 	RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSAMD5],
    207  1.1  christos 				    DST_ALG_RSAMD5));
    208  1.1  christos #endif
    209  1.1  christos 	RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA1],
    210  1.1  christos 				    DST_ALG_RSASHA1));
    211  1.1  christos 	RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1],
    212  1.1  christos 				    DST_ALG_NSEC3RSASHA1));
    213  1.1  christos 	RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA256],
    214  1.1  christos 				    DST_ALG_RSASHA256));
    215  1.1  christos 	RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA512],
    216  1.1  christos 				    DST_ALG_RSASHA512));
    217  1.1  christos #if defined(HAVE_OPENSSL_DSA) && !defined(PK11_DSA_DISABLE)
    218  1.1  christos 	RETERR(dst__openssldsa_init(&dst_t_func[DST_ALG_DSA]));
    219  1.1  christos 	RETERR(dst__openssldsa_init(&dst_t_func[DST_ALG_NSEC3DSA]));
    220  1.1  christos #endif
    221  1.1  christos #ifndef PK11_DH_DISABLE
    222  1.1  christos 	RETERR(dst__openssldh_init(&dst_t_func[DST_ALG_DH]));
    223  1.1  christos #endif
    224  1.1  christos #ifdef HAVE_OPENSSL_GOST
    225  1.1  christos 	RETERR(dst__opensslgost_init(&dst_t_func[DST_ALG_ECCGOST]));
    226  1.1  christos #endif
    227  1.1  christos #ifdef HAVE_OPENSSL_ECDSA
    228  1.1  christos 	RETERR(dst__opensslecdsa_init(&dst_t_func[DST_ALG_ECDSA256]));
    229  1.1  christos 	RETERR(dst__opensslecdsa_init(&dst_t_func[DST_ALG_ECDSA384]));
    230  1.1  christos #endif
    231  1.1  christos #ifdef HAVE_OPENSSL_ED25519
    232  1.1  christos 	RETERR(dst__openssleddsa_init(&dst_t_func[DST_ALG_ED25519]));
    233  1.1  christos #endif
    234  1.1  christos #ifdef HAVE_OPENSSL_ED448
    235  1.1  christos 	RETERR(dst__openssleddsa_init(&dst_t_func[DST_ALG_ED448]));
    236  1.1  christos #endif
    237  1.1  christos #elif PKCS11CRYPTO
    238  1.1  christos 	RETERR(dst__pkcs11_init(mctx, engine));
    239  1.1  christos #ifndef PK11_MD5_DISABLE
    240  1.1  christos 	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSAMD5]));
    241  1.1  christos #endif
    242  1.1  christos 	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA1]));
    243  1.1  christos 	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1]));
    244  1.1  christos 	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA256]));
    245  1.1  christos 	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA512]));
    246  1.1  christos #ifndef PK11_DSA_DISABLE
    247  1.1  christos 	RETERR(dst__pkcs11dsa_init(&dst_t_func[DST_ALG_DSA]));
    248  1.1  christos 	RETERR(dst__pkcs11dsa_init(&dst_t_func[DST_ALG_NSEC3DSA]));
    249  1.1  christos #endif
    250  1.1  christos #ifndef PK11_DH_DISABLE
    251  1.1  christos 	RETERR(dst__pkcs11dh_init(&dst_t_func[DST_ALG_DH]));
    252  1.1  christos #endif
    253  1.1  christos #ifdef HAVE_PKCS11_ECDSA
    254  1.1  christos 	RETERR(dst__pkcs11ecdsa_init(&dst_t_func[DST_ALG_ECDSA256]));
    255  1.1  christos 	RETERR(dst__pkcs11ecdsa_init(&dst_t_func[DST_ALG_ECDSA384]));
    256  1.1  christos #endif
    257  1.1  christos #ifdef HAVE_PKCS11_ED25519
    258  1.1  christos 	RETERR(dst__pkcs11eddsa_init(&dst_t_func[DST_ALG_ED25519]));
    259  1.1  christos #endif
    260  1.1  christos #ifdef HAVE_PKCS11_ED448
    261  1.1  christos 	RETERR(dst__pkcs11eddsa_init(&dst_t_func[DST_ALG_ED448]));
    262  1.1  christos #endif
    263  1.1  christos #ifdef HAVE_PKCS11_GOST
    264  1.1  christos 	RETERR(dst__pkcs11gost_init(&dst_t_func[DST_ALG_ECCGOST]));
    265  1.1  christos #endif
    266  1.1  christos #endif /* if OPENSSL, elif PKCS11CRYPTO */
    267  1.1  christos #ifdef GSSAPI
    268  1.1  christos 	RETERR(dst__gssapi_init(&dst_t_func[DST_ALG_GSSAPI]));
    269  1.1  christos #endif
    270  1.1  christos #if defined(OPENSSL) || defined(PKCS11CRYPTO)
    271  1.1  christos #ifdef ISC_PLATFORM_CRYPTORANDOM
    272  1.1  christos 	if (dst_entropy_pool != NULL) {
    273  1.1  christos 		isc_entropy_sethook(dst_random_getdata);
    274  1.1  christos 	}
    275  1.1  christos #endif
    276  1.1  christos #endif /* defined(OPENSSL) || defined(PKCS11CRYPTO) */
    277  1.1  christos 	dst_initialized = ISC_TRUE;
    278  1.1  christos 	return (ISC_R_SUCCESS);
    279  1.1  christos 
    280  1.1  christos  out:
    281  1.1  christos 	/* avoid immediate crash! */
    282  1.1  christos 	dst_initialized = ISC_TRUE;
    283  1.1  christos 	dst_lib_destroy();
    284  1.1  christos 	return (result);
    285  1.1  christos }
    286  1.1  christos 
    287  1.1  christos void
    288  1.1  christos dst_lib_destroy(void) {
    289  1.1  christos 	int i;
    290  1.1  christos 	RUNTIME_CHECK(dst_initialized == ISC_TRUE);
    291  1.1  christos 	dst_initialized = ISC_FALSE;
    292  1.1  christos 
    293  1.1  christos 	for (i = 0; i < DST_MAX_ALGS; i++)
    294  1.1  christos 		if (dst_t_func[i] != NULL && dst_t_func[i]->cleanup != NULL)
    295  1.1  christos 			dst_t_func[i]->cleanup();
    296  1.1  christos #if defined(OPENSSL) || defined(PKCS11CRYPTO)
    297  1.1  christos #ifdef ISC_PLATFORM_CRYPTORANDOM
    298  1.1  christos 	if (dst_entropy_pool != NULL) {
    299  1.1  christos 		isc_entropy_usehook(dst_entropy_pool, ISC_FALSE);
    300  1.1  christos 		isc_entropy_sethook(NULL);
    301  1.1  christos 	}
    302  1.1  christos #endif
    303  1.1  christos #ifdef OPENSSL
    304  1.1  christos 	dst__openssl_destroy();
    305  1.1  christos #elif PKCS11CRYPTO
    306  1.1  christos 	(void) dst__pkcs11_destroy();
    307  1.1  christos #endif /* if OPENSSL, elif PKCS11CRYPTO */
    308  1.1  christos #endif /* defined(OPENSSL) || defined(PKCS11CRYPTO) */
    309  1.1  christos 	if (dst__memory_pool != NULL)
    310  1.1  christos 		isc_mem_detach(&dst__memory_pool);
    311  1.1  christos 	if (dst_entropy_pool != NULL)
    312  1.1  christos 		isc_entropy_detach(&dst_entropy_pool);
    313  1.1  christos }
    314  1.1  christos 
    315  1.1  christos isc_boolean_t
    316  1.1  christos dst_algorithm_supported(unsigned int alg) {
    317  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    318  1.1  christos 
    319  1.1  christos 	if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
    320  1.1  christos 		return (ISC_FALSE);
    321  1.1  christos 	return (ISC_TRUE);
    322  1.1  christos }
    323  1.1  christos 
    324  1.1  christos isc_boolean_t
    325  1.1  christos dst_ds_digest_supported(unsigned int digest_type) {
    326  1.1  christos #if defined(HAVE_OPENSSL_GOST) || defined(HAVE_PKCS11_GOST)
    327  1.1  christos 	return  (ISC_TF(digest_type == DNS_DSDIGEST_SHA1 ||
    328  1.1  christos 			digest_type == DNS_DSDIGEST_SHA256 ||
    329  1.1  christos 			digest_type == DNS_DSDIGEST_GOST ||
    330  1.1  christos 			digest_type == DNS_DSDIGEST_SHA384));
    331  1.1  christos #else
    332  1.1  christos 	return  (ISC_TF(digest_type == DNS_DSDIGEST_SHA1 ||
    333  1.1  christos 			digest_type == DNS_DSDIGEST_SHA256 ||
    334  1.1  christos 			digest_type == DNS_DSDIGEST_SHA384));
    335  1.1  christos #endif
    336  1.1  christos }
    337  1.1  christos 
    338  1.1  christos isc_result_t
    339  1.1  christos dst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp) {
    340  1.1  christos 	return (dst_context_create4(key, mctx, DNS_LOGCATEGORY_GENERAL,
    341  1.1  christos 				    ISC_TRUE, 0, dctxp));
    342  1.1  christos }
    343  1.1  christos 
    344  1.1  christos isc_result_t
    345  1.1  christos dst_context_create2(dst_key_t *key, isc_mem_t *mctx,
    346  1.1  christos 		    isc_logcategory_t *category, dst_context_t **dctxp)
    347  1.1  christos {
    348  1.1  christos 	return (dst_context_create4(key, mctx, category, ISC_TRUE, 0, dctxp));
    349  1.1  christos }
    350  1.1  christos 
    351  1.1  christos isc_result_t
    352  1.1  christos dst_context_create3(dst_key_t *key, isc_mem_t *mctx,
    353  1.1  christos 		    isc_logcategory_t *category, isc_boolean_t useforsigning,
    354  1.1  christos 		    dst_context_t **dctxp)
    355  1.1  christos {
    356  1.1  christos 	return (dst_context_create4(key, mctx, category,
    357  1.1  christos 				    useforsigning, 0, dctxp));
    358  1.1  christos }
    359  1.1  christos 
    360  1.1  christos isc_result_t
    361  1.1  christos dst_context_create4(dst_key_t *key, isc_mem_t *mctx,
    362  1.1  christos 		    isc_logcategory_t *category, isc_boolean_t useforsigning,
    363  1.1  christos 		    int maxbits, dst_context_t **dctxp)
    364  1.1  christos {
    365  1.1  christos 	dst_context_t *dctx;
    366  1.1  christos 	isc_result_t result;
    367  1.1  christos 
    368  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    369  1.1  christos 	REQUIRE(VALID_KEY(key));
    370  1.1  christos 	REQUIRE(mctx != NULL);
    371  1.1  christos 	REQUIRE(dctxp != NULL && *dctxp == NULL);
    372  1.1  christos 
    373  1.1  christos 	if (key->func->createctx == NULL &&
    374  1.1  christos 	    key->func->createctx2 == NULL)
    375  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
    376  1.1  christos 	if (key->keydata.generic == NULL)
    377  1.1  christos 		return (DST_R_NULLKEY);
    378  1.1  christos 
    379  1.1  christos 	dctx = isc_mem_get(mctx, sizeof(dst_context_t));
    380  1.1  christos 	if (dctx == NULL)
    381  1.1  christos 		return (ISC_R_NOMEMORY);
    382  1.1  christos 	memset(dctx, 0, sizeof(*dctx));
    383  1.1  christos 	dst_key_attach(key, &dctx->key);
    384  1.1  christos 	isc_mem_attach(mctx, &dctx->mctx);
    385  1.1  christos 	dctx->category = category;
    386  1.1  christos 	if (useforsigning)
    387  1.1  christos 		dctx->use = DO_SIGN;
    388  1.1  christos 	else
    389  1.1  christos 		dctx->use = DO_VERIFY;
    390  1.1  christos 	if (key->func->createctx2 != NULL)
    391  1.1  christos 		result = key->func->createctx2(key, maxbits, dctx);
    392  1.1  christos 	else
    393  1.1  christos 		result = key->func->createctx(key, dctx);
    394  1.1  christos 	if (result != ISC_R_SUCCESS) {
    395  1.1  christos 		if (dctx->key != NULL)
    396  1.1  christos 			dst_key_free(&dctx->key);
    397  1.1  christos 		isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(dst_context_t));
    398  1.1  christos 		return (result);
    399  1.1  christos 	}
    400  1.1  christos 	dctx->magic = CTX_MAGIC;
    401  1.1  christos 	*dctxp = dctx;
    402  1.1  christos 	return (ISC_R_SUCCESS);
    403  1.1  christos }
    404  1.1  christos 
    405  1.1  christos void
    406  1.1  christos dst_context_destroy(dst_context_t **dctxp) {
    407  1.1  christos 	dst_context_t *dctx;
    408  1.1  christos 
    409  1.1  christos 	REQUIRE(dctxp != NULL && VALID_CTX(*dctxp));
    410  1.1  christos 
    411  1.1  christos 	dctx = *dctxp;
    412  1.1  christos 	INSIST(dctx->key->func->destroyctx != NULL);
    413  1.1  christos 	dctx->key->func->destroyctx(dctx);
    414  1.1  christos 	if (dctx->key != NULL)
    415  1.1  christos 		dst_key_free(&dctx->key);
    416  1.1  christos 	dctx->magic = 0;
    417  1.1  christos 	isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(dst_context_t));
    418  1.1  christos 	*dctxp = NULL;
    419  1.1  christos }
    420  1.1  christos 
    421  1.1  christos isc_result_t
    422  1.1  christos dst_context_adddata(dst_context_t *dctx, const isc_region_t *data) {
    423  1.1  christos 	REQUIRE(VALID_CTX(dctx));
    424  1.1  christos 	REQUIRE(data != NULL);
    425  1.1  christos 	INSIST(dctx->key->func->adddata != NULL);
    426  1.1  christos 
    427  1.1  christos 	return (dctx->key->func->adddata(dctx, data));
    428  1.1  christos }
    429  1.1  christos 
    430  1.1  christos isc_result_t
    431  1.1  christos dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig) {
    432  1.1  christos 	dst_key_t *key;
    433  1.1  christos 
    434  1.1  christos 	REQUIRE(VALID_CTX(dctx));
    435  1.1  christos 	REQUIRE(sig != NULL);
    436  1.1  christos 
    437  1.1  christos 	key = dctx->key;
    438  1.1  christos 	CHECKALG(key->key_alg);
    439  1.1  christos 	if (key->keydata.generic == NULL)
    440  1.1  christos 		return (DST_R_NULLKEY);
    441  1.1  christos 
    442  1.1  christos 	if (key->func->sign == NULL)
    443  1.1  christos 		return (DST_R_NOTPRIVATEKEY);
    444  1.1  christos 	if (key->func->isprivate == NULL ||
    445  1.1  christos 	    key->func->isprivate(key) == ISC_FALSE)
    446  1.1  christos 		return (DST_R_NOTPRIVATEKEY);
    447  1.1  christos 
    448  1.1  christos 	return (key->func->sign(dctx, sig));
    449  1.1  christos }
    450  1.1  christos 
    451  1.1  christos isc_result_t
    452  1.1  christos dst_context_verify(dst_context_t *dctx, isc_region_t *sig) {
    453  1.1  christos 	REQUIRE(VALID_CTX(dctx));
    454  1.1  christos 	REQUIRE(sig != NULL);
    455  1.1  christos 
    456  1.1  christos 	CHECKALG(dctx->key->key_alg);
    457  1.1  christos 	if (dctx->key->keydata.generic == NULL)
    458  1.1  christos 		return (DST_R_NULLKEY);
    459  1.1  christos 	if (dctx->key->func->verify == NULL)
    460  1.1  christos 		return (DST_R_NOTPUBLICKEY);
    461  1.1  christos 
    462  1.1  christos 	return (dctx->key->func->verify(dctx, sig));
    463  1.1  christos }
    464  1.1  christos 
    465  1.1  christos isc_result_t
    466  1.1  christos dst_context_verify2(dst_context_t *dctx, unsigned int maxbits,
    467  1.1  christos 		    isc_region_t *sig)
    468  1.1  christos {
    469  1.1  christos 	REQUIRE(VALID_CTX(dctx));
    470  1.1  christos 	REQUIRE(sig != NULL);
    471  1.1  christos 
    472  1.1  christos 	CHECKALG(dctx->key->key_alg);
    473  1.1  christos 	if (dctx->key->keydata.generic == NULL)
    474  1.1  christos 		return (DST_R_NULLKEY);
    475  1.1  christos 	if (dctx->key->func->verify == NULL &&
    476  1.1  christos 	    dctx->key->func->verify2 == NULL)
    477  1.1  christos 		return (DST_R_NOTPUBLICKEY);
    478  1.1  christos 
    479  1.1  christos 	return (dctx->key->func->verify2 != NULL ?
    480  1.1  christos 		dctx->key->func->verify2(dctx, maxbits, sig) :
    481  1.1  christos 		dctx->key->func->verify(dctx, sig));
    482  1.1  christos }
    483  1.1  christos 
    484  1.1  christos isc_result_t
    485  1.1  christos dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
    486  1.1  christos 		      isc_buffer_t *secret)
    487  1.1  christos {
    488  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    489  1.1  christos 	REQUIRE(VALID_KEY(pub) && VALID_KEY(priv));
    490  1.1  christos 	REQUIRE(secret != NULL);
    491  1.1  christos 
    492  1.1  christos 	CHECKALG(pub->key_alg);
    493  1.1  christos 	CHECKALG(priv->key_alg);
    494  1.1  christos 
    495  1.1  christos 	if (pub->keydata.generic == NULL || priv->keydata.generic == NULL)
    496  1.1  christos 		return (DST_R_NULLKEY);
    497  1.1  christos 
    498  1.1  christos 	if (pub->key_alg != priv->key_alg ||
    499  1.1  christos 	    pub->func->computesecret == NULL ||
    500  1.1  christos 	    priv->func->computesecret == NULL)
    501  1.1  christos 		return (DST_R_KEYCANNOTCOMPUTESECRET);
    502  1.1  christos 
    503  1.1  christos 	if (dst_key_isprivate(priv) == ISC_FALSE)
    504  1.1  christos 		return (DST_R_NOTPRIVATEKEY);
    505  1.1  christos 
    506  1.1  christos 	return (pub->func->computesecret(pub, priv, secret));
    507  1.1  christos }
    508  1.1  christos 
    509  1.1  christos isc_result_t
    510  1.1  christos dst_key_tofile(const dst_key_t *key, int type, const char *directory) {
    511  1.1  christos 	isc_result_t ret = ISC_R_SUCCESS;
    512  1.1  christos 
    513  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    514  1.1  christos 	REQUIRE(VALID_KEY(key));
    515  1.1  christos 	REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
    516  1.1  christos 
    517  1.1  christos 	CHECKALG(key->key_alg);
    518  1.1  christos 
    519  1.1  christos 	if (key->func->tofile == NULL)
    520  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
    521  1.1  christos 
    522  1.1  christos 	if (type & DST_TYPE_PUBLIC) {
    523  1.1  christos 		ret = write_public_key(key, type, directory);
    524  1.1  christos 		if (ret != ISC_R_SUCCESS)
    525  1.1  christos 			return (ret);
    526  1.1  christos 	}
    527  1.1  christos 
    528  1.1  christos 	if ((type & DST_TYPE_PRIVATE) &&
    529  1.1  christos 	    (key->key_flags & DNS_KEYFLAG_TYPEMASK) != DNS_KEYTYPE_NOKEY)
    530  1.1  christos 		return (key->func->tofile(key, directory));
    531  1.1  christos 	else
    532  1.1  christos 		return (ISC_R_SUCCESS);
    533  1.1  christos }
    534  1.1  christos 
    535  1.1  christos void
    536  1.1  christos dst_key_setexternal(dst_key_t *key, isc_boolean_t value) {
    537  1.1  christos 	key->external = value;
    538  1.1  christos }
    539  1.1  christos 
    540  1.1  christos isc_boolean_t
    541  1.1  christos dst_key_isexternal(dst_key_t *key) {
    542  1.1  christos 	return (key->external);
    543  1.1  christos }
    544  1.1  christos 
    545  1.1  christos isc_result_t
    546  1.1  christos dst_key_getfilename(dns_name_t *name, dns_keytag_t id,
    547  1.1  christos 		    unsigned int alg, int type, const char *directory,
    548  1.1  christos 		    isc_mem_t *mctx, isc_buffer_t *buf)
    549  1.1  christos {
    550  1.1  christos 	isc_result_t result;
    551  1.1  christos 
    552  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    553  1.1  christos 	REQUIRE(dns_name_isabsolute(name));
    554  1.1  christos 	REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
    555  1.1  christos 	REQUIRE(mctx != NULL);
    556  1.1  christos 	REQUIRE(buf != NULL);
    557  1.1  christos 
    558  1.1  christos 	CHECKALG(alg);
    559  1.1  christos 
    560  1.1  christos 	result = buildfilename(name, id, alg, type, directory, buf);
    561  1.1  christos 	if (result == ISC_R_SUCCESS) {
    562  1.1  christos 		if (isc_buffer_availablelength(buf) > 0)
    563  1.1  christos 			isc_buffer_putuint8(buf, 0);
    564  1.1  christos 		else
    565  1.1  christos 			result = ISC_R_NOSPACE;
    566  1.1  christos 	}
    567  1.1  christos 
    568  1.1  christos 	return (result);
    569  1.1  christos }
    570  1.1  christos 
    571  1.1  christos isc_result_t
    572  1.1  christos dst_key_fromfile(dns_name_t *name, dns_keytag_t id,
    573  1.1  christos 		 unsigned int alg, int type, const char *directory,
    574  1.1  christos 		 isc_mem_t *mctx, dst_key_t **keyp)
    575  1.1  christos {
    576  1.1  christos 	isc_result_t result;
    577  1.1  christos 	char filename[ISC_DIR_NAMEMAX];
    578  1.1  christos 	isc_buffer_t buf;
    579  1.1  christos 	dst_key_t *key;
    580  1.1  christos 
    581  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    582  1.1  christos 	REQUIRE(dns_name_isabsolute(name));
    583  1.1  christos 	REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
    584  1.1  christos 	REQUIRE(mctx != NULL);
    585  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
    586  1.1  christos 
    587  1.1  christos 	CHECKALG(alg);
    588  1.1  christos 
    589  1.1  christos 	key = NULL;
    590  1.1  christos 
    591  1.1  christos 	isc_buffer_init(&buf, filename, ISC_DIR_NAMEMAX);
    592  1.1  christos 	result = dst_key_getfilename(name, id, alg, type, NULL, mctx, &buf);
    593  1.1  christos 	if (result != ISC_R_SUCCESS)
    594  1.1  christos 		goto out;
    595  1.1  christos 
    596  1.1  christos 	result = dst_key_fromnamedfile(filename, directory, type, mctx, &key);
    597  1.1  christos 	if (result != ISC_R_SUCCESS)
    598  1.1  christos 		goto out;
    599  1.1  christos 
    600  1.1  christos 	result = computeid(key);
    601  1.1  christos 	if (result != ISC_R_SUCCESS)
    602  1.1  christos 		goto out;
    603  1.1  christos 
    604  1.1  christos 	if (!dns_name_equal(name, key->key_name) || id != key->key_id ||
    605  1.1  christos 	    alg != key->key_alg) {
    606  1.1  christos 		result = DST_R_INVALIDPRIVATEKEY;
    607  1.1  christos 		goto out;
    608  1.1  christos 	}
    609  1.1  christos 
    610  1.1  christos 	*keyp = key;
    611  1.1  christos 	result = ISC_R_SUCCESS;
    612  1.1  christos 
    613  1.1  christos  out:
    614  1.1  christos 	if ((key != NULL) && (result != ISC_R_SUCCESS))
    615  1.1  christos 		dst_key_free(&key);
    616  1.1  christos 
    617  1.1  christos 	return (result);
    618  1.1  christos }
    619  1.1  christos 
    620  1.1  christos isc_result_t
    621  1.1  christos dst_key_fromnamedfile(const char *filename, const char *dirname,
    622  1.1  christos 		      int type, isc_mem_t *mctx, dst_key_t **keyp)
    623  1.1  christos {
    624  1.1  christos 	isc_result_t result;
    625  1.1  christos 	dst_key_t *pubkey = NULL, *key = NULL;
    626  1.1  christos 	char *newfilename = NULL;
    627  1.1  christos 	int newfilenamelen = 0;
    628  1.1  christos 	isc_lex_t *lex = NULL;
    629  1.1  christos 
    630  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    631  1.1  christos 	REQUIRE(filename != NULL);
    632  1.1  christos 	REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
    633  1.1  christos 	REQUIRE(mctx != NULL);
    634  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
    635  1.1  christos 
    636  1.1  christos 	/* If an absolute path is specified, don't use the key directory */
    637  1.1  christos #ifndef WIN32
    638  1.1  christos 	if (filename[0] == '/')
    639  1.1  christos 		dirname = NULL;
    640  1.1  christos #else /* WIN32 */
    641  1.1  christos 	if (filename[0] == '/' || filename[0] == '\\')
    642  1.1  christos 		dirname = NULL;
    643  1.1  christos #endif
    644  1.1  christos 
    645  1.1  christos 	newfilenamelen = strlen(filename) + 5;
    646  1.1  christos 	if (dirname != NULL)
    647  1.1  christos 		newfilenamelen += strlen(dirname) + 1;
    648  1.1  christos 	newfilename = isc_mem_get(mctx, newfilenamelen);
    649  1.1  christos 	if (newfilename == NULL)
    650  1.1  christos 		return (ISC_R_NOMEMORY);
    651  1.1  christos 	result = addsuffix(newfilename, newfilenamelen,
    652  1.1  christos 			   dirname, filename, ".key");
    653  1.1  christos 	INSIST(result == ISC_R_SUCCESS);
    654  1.1  christos 
    655  1.1  christos 	result = dst_key_read_public(newfilename, type, mctx, &pubkey);
    656  1.1  christos 	isc_mem_put(mctx, newfilename, newfilenamelen);
    657  1.1  christos 	newfilename = NULL;
    658  1.1  christos 	RETERR(result);
    659  1.1  christos 
    660  1.1  christos 	if ((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) == DST_TYPE_PUBLIC ||
    661  1.1  christos 	    (pubkey->key_flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
    662  1.1  christos 		result = computeid(pubkey);
    663  1.1  christos 		if (result != ISC_R_SUCCESS) {
    664  1.1  christos 			dst_key_free(&pubkey);
    665  1.1  christos 			return (result);
    666  1.1  christos 		}
    667  1.1  christos 
    668  1.1  christos 		*keyp = pubkey;
    669  1.1  christos 		return (ISC_R_SUCCESS);
    670  1.1  christos 	}
    671  1.1  christos 
    672  1.1  christos 	result = algorithm_status(pubkey->key_alg);
    673  1.1  christos 	if (result != ISC_R_SUCCESS) {
    674  1.1  christos 		dst_key_free(&pubkey);
    675  1.1  christos 		return (result);
    676  1.1  christos 	}
    677  1.1  christos 
    678  1.1  christos 	key = get_key_struct(pubkey->key_name, pubkey->key_alg,
    679  1.1  christos 			     pubkey->key_flags, pubkey->key_proto, 0,
    680  1.1  christos 			     pubkey->key_class, pubkey->key_ttl, mctx);
    681  1.1  christos 	if (key == NULL) {
    682  1.1  christos 		dst_key_free(&pubkey);
    683  1.1  christos 		return (ISC_R_NOMEMORY);
    684  1.1  christos 	}
    685  1.1  christos 
    686  1.1  christos 	if (key->func->parse == NULL)
    687  1.1  christos 		RETERR(DST_R_UNSUPPORTEDALG);
    688  1.1  christos 
    689  1.1  christos 	newfilenamelen = strlen(filename) + 9;
    690  1.1  christos 	if (dirname != NULL)
    691  1.1  christos 		newfilenamelen += strlen(dirname) + 1;
    692  1.1  christos 	newfilename = isc_mem_get(mctx, newfilenamelen);
    693  1.1  christos 	if (newfilename == NULL)
    694  1.1  christos 		RETERR(ISC_R_NOMEMORY);
    695  1.1  christos 	result = addsuffix(newfilename, newfilenamelen,
    696  1.1  christos 			   dirname, filename, ".private");
    697  1.1  christos 	INSIST(result == ISC_R_SUCCESS);
    698  1.1  christos 
    699  1.1  christos 	RETERR(isc_lex_create(mctx, 1500, &lex));
    700  1.1  christos 	RETERR(isc_lex_openfile(lex, newfilename));
    701  1.1  christos 	isc_mem_put(mctx, newfilename, newfilenamelen);
    702  1.1  christos 
    703  1.1  christos 	RETERR(key->func->parse(key, lex, pubkey));
    704  1.1  christos 	isc_lex_destroy(&lex);
    705  1.1  christos 
    706  1.1  christos 	RETERR(computeid(key));
    707  1.1  christos 
    708  1.1  christos 	if (pubkey->key_id != key->key_id)
    709  1.1  christos 		RETERR(DST_R_INVALIDPRIVATEKEY);
    710  1.1  christos 	dst_key_free(&pubkey);
    711  1.1  christos 
    712  1.1  christos 	*keyp = key;
    713  1.1  christos 	return (ISC_R_SUCCESS);
    714  1.1  christos 
    715  1.1  christos  out:
    716  1.1  christos 	if (pubkey != NULL)
    717  1.1  christos 		dst_key_free(&pubkey);
    718  1.1  christos 	if (newfilename != NULL)
    719  1.1  christos 		isc_mem_put(mctx, newfilename, newfilenamelen);
    720  1.1  christos 	if (lex != NULL)
    721  1.1  christos 		isc_lex_destroy(&lex);
    722  1.1  christos 	if (key != NULL)
    723  1.1  christos 		dst_key_free(&key);
    724  1.1  christos 	return (result);
    725  1.1  christos }
    726  1.1  christos 
    727  1.1  christos isc_result_t
    728  1.1  christos dst_key_todns(const dst_key_t *key, isc_buffer_t *target) {
    729  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    730  1.1  christos 	REQUIRE(VALID_KEY(key));
    731  1.1  christos 	REQUIRE(target != NULL);
    732  1.1  christos 
    733  1.1  christos 	CHECKALG(key->key_alg);
    734  1.1  christos 
    735  1.1  christos 	if (key->func->todns == NULL)
    736  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
    737  1.1  christos 
    738  1.1  christos 	if (isc_buffer_availablelength(target) < 4)
    739  1.1  christos 		return (ISC_R_NOSPACE);
    740  1.1  christos 	isc_buffer_putuint16(target, (isc_uint16_t)(key->key_flags & 0xffff));
    741  1.1  christos 	isc_buffer_putuint8(target, (isc_uint8_t)key->key_proto);
    742  1.1  christos 	isc_buffer_putuint8(target, (isc_uint8_t)key->key_alg);
    743  1.1  christos 
    744  1.1  christos 	if (key->key_flags & DNS_KEYFLAG_EXTENDED) {
    745  1.1  christos 		if (isc_buffer_availablelength(target) < 2)
    746  1.1  christos 			return (ISC_R_NOSPACE);
    747  1.1  christos 		isc_buffer_putuint16(target,
    748  1.1  christos 				     (isc_uint16_t)((key->key_flags >> 16)
    749  1.1  christos 						    & 0xffff));
    750  1.1  christos 	}
    751  1.1  christos 
    752  1.1  christos 	if (key->keydata.generic == NULL) /*%< NULL KEY */
    753  1.1  christos 		return (ISC_R_SUCCESS);
    754  1.1  christos 
    755  1.1  christos 	return (key->func->todns(key, target));
    756  1.1  christos }
    757  1.1  christos 
    758  1.1  christos isc_result_t
    759  1.1  christos dst_key_fromdns(const dns_name_t *name, dns_rdataclass_t rdclass,
    760  1.1  christos 		isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
    761  1.1  christos {
    762  1.1  christos 	isc_uint8_t alg, proto;
    763  1.1  christos 	isc_uint32_t flags, extflags;
    764  1.1  christos 	dst_key_t *key = NULL;
    765  1.1  christos 	dns_keytag_t id, rid;
    766  1.1  christos 	isc_region_t r;
    767  1.1  christos 	isc_result_t result;
    768  1.1  christos 
    769  1.1  christos 	REQUIRE(dst_initialized);
    770  1.1  christos 
    771  1.1  christos 	isc_buffer_remainingregion(source, &r);
    772  1.1  christos 
    773  1.1  christos 	if (isc_buffer_remaininglength(source) < 4)
    774  1.1  christos 		return (DST_R_INVALIDPUBLICKEY);
    775  1.1  christos 	flags = isc_buffer_getuint16(source);
    776  1.1  christos 	proto = isc_buffer_getuint8(source);
    777  1.1  christos 	alg = isc_buffer_getuint8(source);
    778  1.1  christos 
    779  1.1  christos 	id = dst_region_computeid(&r, alg);
    780  1.1  christos 	rid = dst_region_computerid(&r, alg);
    781  1.1  christos 
    782  1.1  christos 	if (flags & DNS_KEYFLAG_EXTENDED) {
    783  1.1  christos 		if (isc_buffer_remaininglength(source) < 2)
    784  1.1  christos 			return (DST_R_INVALIDPUBLICKEY);
    785  1.1  christos 		extflags = isc_buffer_getuint16(source);
    786  1.1  christos 		flags |= (extflags << 16);
    787  1.1  christos 	}
    788  1.1  christos 
    789  1.1  christos 	result = frombuffer(name, alg, flags, proto, rdclass, source,
    790  1.1  christos 			    mctx, &key);
    791  1.1  christos 	if (result != ISC_R_SUCCESS)
    792  1.1  christos 		return (result);
    793  1.1  christos 	key->key_id = id;
    794  1.1  christos 	key->key_rid = rid;
    795  1.1  christos 
    796  1.1  christos 	*keyp = key;
    797  1.1  christos 	return (ISC_R_SUCCESS);
    798  1.1  christos }
    799  1.1  christos 
    800  1.1  christos isc_result_t
    801  1.1  christos dst_key_frombuffer(const dns_name_t *name, unsigned int alg,
    802  1.1  christos 		   unsigned int flags, unsigned int protocol,
    803  1.1  christos 		   dns_rdataclass_t rdclass,
    804  1.1  christos 		   isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
    805  1.1  christos {
    806  1.1  christos 	dst_key_t *key = NULL;
    807  1.1  christos 	isc_result_t result;
    808  1.1  christos 
    809  1.1  christos 	REQUIRE(dst_initialized);
    810  1.1  christos 
    811  1.1  christos 	result = frombuffer(name, alg, flags, protocol, rdclass, source,
    812  1.1  christos 			    mctx, &key);
    813  1.1  christos 	if (result != ISC_R_SUCCESS)
    814  1.1  christos 		return (result);
    815  1.1  christos 
    816  1.1  christos 	result = computeid(key);
    817  1.1  christos 	if (result != ISC_R_SUCCESS) {
    818  1.1  christos 		dst_key_free(&key);
    819  1.1  christos 		return (result);
    820  1.1  christos 	}
    821  1.1  christos 
    822  1.1  christos 	*keyp = key;
    823  1.1  christos 	return (ISC_R_SUCCESS);
    824  1.1  christos }
    825  1.1  christos 
    826  1.1  christos isc_result_t
    827  1.1  christos dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target) {
    828  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    829  1.1  christos 	REQUIRE(VALID_KEY(key));
    830  1.1  christos 	REQUIRE(target != NULL);
    831  1.1  christos 
    832  1.1  christos 	CHECKALG(key->key_alg);
    833  1.1  christos 
    834  1.1  christos 	if (key->func->todns == NULL)
    835  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
    836  1.1  christos 
    837  1.1  christos 	return (key->func->todns(key, target));
    838  1.1  christos }
    839  1.1  christos 
    840  1.1  christos isc_result_t
    841  1.1  christos dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer) {
    842  1.1  christos 	isc_lex_t *lex = NULL;
    843  1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
    844  1.1  christos 
    845  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    846  1.1  christos 	REQUIRE(VALID_KEY(key));
    847  1.1  christos 	REQUIRE(!dst_key_isprivate(key));
    848  1.1  christos 	REQUIRE(buffer != NULL);
    849  1.1  christos 
    850  1.1  christos 	if (key->func->parse == NULL)
    851  1.1  christos 		RETERR(DST_R_UNSUPPORTEDALG);
    852  1.1  christos 
    853  1.1  christos 	RETERR(isc_lex_create(key->mctx, 1500, &lex));
    854  1.1  christos 	RETERR(isc_lex_openbuffer(lex, buffer));
    855  1.1  christos 	RETERR(key->func->parse(key, lex, NULL));
    856  1.1  christos  out:
    857  1.1  christos 	if (lex != NULL)
    858  1.1  christos 		isc_lex_destroy(&lex);
    859  1.1  christos 	return (result);
    860  1.1  christos }
    861  1.1  christos 
    862  1.1  christos gss_ctx_id_t
    863  1.1  christos dst_key_getgssctx(const dst_key_t *key)
    864  1.1  christos {
    865  1.1  christos 	REQUIRE(key != NULL);
    866  1.1  christos 
    867  1.1  christos 	return (key->keydata.gssctx);
    868  1.1  christos }
    869  1.1  christos 
    870  1.1  christos isc_result_t
    871  1.1  christos dst_key_fromgssapi(const dns_name_t *name, gss_ctx_id_t gssctx,
    872  1.1  christos 		   isc_mem_t *mctx, dst_key_t **keyp, isc_region_t *intoken)
    873  1.1  christos {
    874  1.1  christos 	dst_key_t *key;
    875  1.1  christos 	isc_result_t result;
    876  1.1  christos 
    877  1.1  christos 	REQUIRE(gssctx != NULL);
    878  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
    879  1.1  christos 
    880  1.1  christos 	key = get_key_struct(name, DST_ALG_GSSAPI, 0, DNS_KEYPROTO_DNSSEC,
    881  1.1  christos 			     0, dns_rdataclass_in, 0, mctx);
    882  1.1  christos 	if (key == NULL)
    883  1.1  christos 		return (ISC_R_NOMEMORY);
    884  1.1  christos 
    885  1.1  christos 	if (intoken != NULL) {
    886  1.1  christos 		/*
    887  1.1  christos 		 * Keep the token for use by external ssu rules. They may need
    888  1.1  christos 		 * to examine the PAC in the kerberos ticket.
    889  1.1  christos 		 */
    890  1.1  christos 		RETERR(isc_buffer_allocate(key->mctx, &key->key_tkeytoken,
    891  1.1  christos 		       intoken->length));
    892  1.1  christos 		RETERR(isc_buffer_copyregion(key->key_tkeytoken, intoken));
    893  1.1  christos 	}
    894  1.1  christos 
    895  1.1  christos 	key->keydata.gssctx = gssctx;
    896  1.1  christos 	*keyp = key;
    897  1.1  christos 	result = ISC_R_SUCCESS;
    898  1.1  christos out:
    899  1.1  christos 	return result;
    900  1.1  christos }
    901  1.1  christos 
    902  1.1  christos isc_result_t
    903  1.1  christos dst_key_buildinternal(const dns_name_t *name, unsigned int alg,
    904  1.1  christos 		      unsigned int bits, unsigned int flags,
    905  1.1  christos 		      unsigned int protocol, dns_rdataclass_t rdclass,
    906  1.1  christos 		      void *data, isc_mem_t *mctx, dst_key_t **keyp)
    907  1.1  christos {
    908  1.1  christos 	dst_key_t *key;
    909  1.1  christos 	isc_result_t result;
    910  1.1  christos 
    911  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    912  1.1  christos 	REQUIRE(dns_name_isabsolute(name));
    913  1.1  christos 	REQUIRE(mctx != NULL);
    914  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
    915  1.1  christos 	REQUIRE(data != NULL);
    916  1.1  christos 
    917  1.1  christos 	CHECKALG(alg);
    918  1.1  christos 
    919  1.1  christos 	key = get_key_struct(name, alg, flags, protocol, bits, rdclass,
    920  1.1  christos 			     0, mctx);
    921  1.1  christos 	if (key == NULL)
    922  1.1  christos 		return (ISC_R_NOMEMORY);
    923  1.1  christos 
    924  1.1  christos 	key->keydata.generic = data;
    925  1.1  christos 
    926  1.1  christos 	result = computeid(key);
    927  1.1  christos 	if (result != ISC_R_SUCCESS) {
    928  1.1  christos 		dst_key_free(&key);
    929  1.1  christos 		return (result);
    930  1.1  christos 	}
    931  1.1  christos 
    932  1.1  christos 	*keyp = key;
    933  1.1  christos 	return (ISC_R_SUCCESS);
    934  1.1  christos }
    935  1.1  christos 
    936  1.1  christos isc_result_t
    937  1.1  christos dst_key_fromlabel(const dns_name_t *name, int alg, unsigned int flags,
    938  1.1  christos 		  unsigned int protocol, dns_rdataclass_t rdclass,
    939  1.1  christos 		  const char *engine, const char *label, const char *pin,
    940  1.1  christos 		  isc_mem_t *mctx, dst_key_t **keyp)
    941  1.1  christos {
    942  1.1  christos 	dst_key_t *key;
    943  1.1  christos 	isc_result_t result;
    944  1.1  christos 
    945  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
    946  1.1  christos 	REQUIRE(dns_name_isabsolute(name));
    947  1.1  christos 	REQUIRE(mctx != NULL);
    948  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
    949  1.1  christos 	REQUIRE(label != NULL);
    950  1.1  christos 
    951  1.1  christos 	CHECKALG(alg);
    952  1.1  christos 
    953  1.1  christos 	key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
    954  1.1  christos 	if (key == NULL)
    955  1.1  christos 		return (ISC_R_NOMEMORY);
    956  1.1  christos 
    957  1.1  christos 	if (key->func->fromlabel == NULL) {
    958  1.1  christos 		dst_key_free(&key);
    959  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
    960  1.1  christos 	}
    961  1.1  christos 
    962  1.1  christos 	result = key->func->fromlabel(key, engine, label, pin);
    963  1.1  christos 	if (result != ISC_R_SUCCESS) {
    964  1.1  christos 		dst_key_free(&key);
    965  1.1  christos 		return (result);
    966  1.1  christos 	}
    967  1.1  christos 
    968  1.1  christos 	result = computeid(key);
    969  1.1  christos 	if (result != ISC_R_SUCCESS) {
    970  1.1  christos 		dst_key_free(&key);
    971  1.1  christos 		return (result);
    972  1.1  christos 	}
    973  1.1  christos 
    974  1.1  christos 	*keyp = key;
    975  1.1  christos 	return (ISC_R_SUCCESS);
    976  1.1  christos }
    977  1.1  christos 
    978  1.1  christos isc_result_t
    979  1.1  christos dst_key_generate(const dns_name_t *name, unsigned int alg,
    980  1.1  christos 		 unsigned int bits, unsigned int param,
    981  1.1  christos 		 unsigned int flags, unsigned int protocol,
    982  1.1  christos 		 dns_rdataclass_t rdclass,
    983  1.1  christos 		 isc_mem_t *mctx, dst_key_t **keyp)
    984  1.1  christos {
    985  1.1  christos 	return (dst_key_generate2(name, alg, bits, param, flags, protocol,
    986  1.1  christos 				  rdclass, mctx, keyp, NULL));
    987  1.1  christos }
    988  1.1  christos 
    989  1.1  christos isc_result_t
    990  1.1  christos dst_key_generate2(const dns_name_t *name, unsigned int alg,
    991  1.1  christos 		  unsigned int bits, unsigned int param,
    992  1.1  christos 		  unsigned int flags, unsigned int protocol,
    993  1.1  christos 		  dns_rdataclass_t rdclass,
    994  1.1  christos 		  isc_mem_t *mctx, dst_key_t **keyp,
    995  1.1  christos 		  void (*callback)(int))
    996  1.1  christos {
    997  1.1  christos 	dst_key_t *key;
    998  1.1  christos 	isc_result_t ret;
    999  1.1  christos 
   1000  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1001  1.1  christos 	REQUIRE(dns_name_isabsolute(name));
   1002  1.1  christos 	REQUIRE(mctx != NULL);
   1003  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
   1004  1.1  christos 
   1005  1.1  christos 	CHECKALG(alg);
   1006  1.1  christos 
   1007  1.1  christos 	key = get_key_struct(name, alg, flags, protocol, bits,
   1008  1.1  christos 			     rdclass, 0, mctx);
   1009  1.1  christos 	if (key == NULL)
   1010  1.1  christos 		return (ISC_R_NOMEMORY);
   1011  1.1  christos 
   1012  1.1  christos 	if (bits == 0) { /*%< NULL KEY */
   1013  1.1  christos 		key->key_flags |= DNS_KEYTYPE_NOKEY;
   1014  1.1  christos 		*keyp = key;
   1015  1.1  christos 		return (ISC_R_SUCCESS);
   1016  1.1  christos 	}
   1017  1.1  christos 
   1018  1.1  christos 	if (key->func->generate == NULL) {
   1019  1.1  christos 		dst_key_free(&key);
   1020  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
   1021  1.1  christos 	}
   1022  1.1  christos 
   1023  1.1  christos 	ret = key->func->generate(key, param, callback);
   1024  1.1  christos 	if (ret != ISC_R_SUCCESS) {
   1025  1.1  christos 		dst_key_free(&key);
   1026  1.1  christos 		return (ret);
   1027  1.1  christos 	}
   1028  1.1  christos 
   1029  1.1  christos 	ret = computeid(key);
   1030  1.1  christos 	if (ret != ISC_R_SUCCESS) {
   1031  1.1  christos 		dst_key_free(&key);
   1032  1.1  christos 		return (ret);
   1033  1.1  christos 	}
   1034  1.1  christos 
   1035  1.1  christos 	*keyp = key;
   1036  1.1  christos 	return (ISC_R_SUCCESS);
   1037  1.1  christos }
   1038  1.1  christos 
   1039  1.1  christos isc_result_t
   1040  1.1  christos dst_key_getnum(const dst_key_t *key, int type, isc_uint32_t *valuep)
   1041  1.1  christos {
   1042  1.1  christos 	REQUIRE(VALID_KEY(key));
   1043  1.1  christos 	REQUIRE(valuep != NULL);
   1044  1.1  christos 	REQUIRE(type <= DST_MAX_NUMERIC);
   1045  1.1  christos 	if (!key->numset[type])
   1046  1.1  christos 		return (ISC_R_NOTFOUND);
   1047  1.1  christos 	*valuep = key->nums[type];
   1048  1.1  christos 	return (ISC_R_SUCCESS);
   1049  1.1  christos }
   1050  1.1  christos 
   1051  1.1  christos void
   1052  1.1  christos dst_key_setnum(dst_key_t *key, int type, isc_uint32_t value)
   1053  1.1  christos {
   1054  1.1  christos 	REQUIRE(VALID_KEY(key));
   1055  1.1  christos 	REQUIRE(type <= DST_MAX_NUMERIC);
   1056  1.1  christos 	key->nums[type] = value;
   1057  1.1  christos 	key->numset[type] = ISC_TRUE;
   1058  1.1  christos }
   1059  1.1  christos 
   1060  1.1  christos void
   1061  1.1  christos dst_key_unsetnum(dst_key_t *key, int type)
   1062  1.1  christos {
   1063  1.1  christos 	REQUIRE(VALID_KEY(key));
   1064  1.1  christos 	REQUIRE(type <= DST_MAX_NUMERIC);
   1065  1.1  christos 	key->numset[type] = ISC_FALSE;
   1066  1.1  christos }
   1067  1.1  christos 
   1068  1.1  christos isc_result_t
   1069  1.1  christos dst_key_gettime(const dst_key_t *key, int type, isc_stdtime_t *timep) {
   1070  1.1  christos 	REQUIRE(VALID_KEY(key));
   1071  1.1  christos 	REQUIRE(timep != NULL);
   1072  1.1  christos 	REQUIRE(type <= DST_MAX_TIMES);
   1073  1.1  christos 	if (!key->timeset[type])
   1074  1.1  christos 		return (ISC_R_NOTFOUND);
   1075  1.1  christos 	*timep = key->times[type];
   1076  1.1  christos 	return (ISC_R_SUCCESS);
   1077  1.1  christos }
   1078  1.1  christos 
   1079  1.1  christos void
   1080  1.1  christos dst_key_settime(dst_key_t *key, int type, isc_stdtime_t when) {
   1081  1.1  christos 	REQUIRE(VALID_KEY(key));
   1082  1.1  christos 	REQUIRE(type <= DST_MAX_TIMES);
   1083  1.1  christos 	key->times[type] = when;
   1084  1.1  christos 	key->timeset[type] = ISC_TRUE;
   1085  1.1  christos }
   1086  1.1  christos 
   1087  1.1  christos void
   1088  1.1  christos dst_key_unsettime(dst_key_t *key, int type) {
   1089  1.1  christos 	REQUIRE(VALID_KEY(key));
   1090  1.1  christos 	REQUIRE(type <= DST_MAX_TIMES);
   1091  1.1  christos 	key->timeset[type] = ISC_FALSE;
   1092  1.1  christos }
   1093  1.1  christos 
   1094  1.1  christos isc_result_t
   1095  1.1  christos dst_key_getprivateformat(const dst_key_t *key, int *majorp, int *minorp) {
   1096  1.1  christos 	REQUIRE(VALID_KEY(key));
   1097  1.1  christos 	REQUIRE(majorp != NULL);
   1098  1.1  christos 	REQUIRE(minorp != NULL);
   1099  1.1  christos 	*majorp = key->fmt_major;
   1100  1.1  christos 	*minorp = key->fmt_minor;
   1101  1.1  christos 	return (ISC_R_SUCCESS);
   1102  1.1  christos }
   1103  1.1  christos 
   1104  1.1  christos void
   1105  1.1  christos dst_key_setprivateformat(dst_key_t *key, int major, int minor) {
   1106  1.1  christos 	REQUIRE(VALID_KEY(key));
   1107  1.1  christos 	key->fmt_major = major;
   1108  1.1  christos 	key->fmt_minor = minor;
   1109  1.1  christos }
   1110  1.1  christos 
   1111  1.1  christos static isc_boolean_t
   1112  1.1  christos comparekeys(const dst_key_t *key1, const dst_key_t *key2,
   1113  1.1  christos 	    isc_boolean_t match_revoked_key,
   1114  1.1  christos 	    isc_boolean_t (*compare)(const dst_key_t *key1,
   1115  1.1  christos 				     const dst_key_t *key2))
   1116  1.1  christos {
   1117  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1118  1.1  christos 	REQUIRE(VALID_KEY(key1));
   1119  1.1  christos 	REQUIRE(VALID_KEY(key2));
   1120  1.1  christos 
   1121  1.1  christos 	if (key1 == key2)
   1122  1.1  christos 		return (ISC_TRUE);
   1123  1.1  christos 
   1124  1.1  christos 	if (key1->key_alg != key2->key_alg)
   1125  1.1  christos 		return (ISC_FALSE);
   1126  1.1  christos 
   1127  1.1  christos 	if (key1->key_id != key2->key_id) {
   1128  1.1  christos 		if (!match_revoked_key)
   1129  1.1  christos 			return (ISC_FALSE);
   1130  1.1  christos #ifndef PK11_MD5_DISABLE
   1131  1.1  christos 		if (key1->key_alg == DST_ALG_RSAMD5)
   1132  1.1  christos 			return (ISC_FALSE);
   1133  1.1  christos #endif
   1134  1.1  christos 		if ((key1->key_flags & DNS_KEYFLAG_REVOKE) ==
   1135  1.1  christos 		    (key2->key_flags & DNS_KEYFLAG_REVOKE))
   1136  1.1  christos 			return (ISC_FALSE);
   1137  1.1  christos 		if (key1->key_id != key2->key_rid &&
   1138  1.1  christos 		    key1->key_rid != key2->key_id)
   1139  1.1  christos 			return (ISC_FALSE);
   1140  1.1  christos 	}
   1141  1.1  christos 
   1142  1.1  christos 	if (compare != NULL)
   1143  1.1  christos 		return (compare(key1, key2));
   1144  1.1  christos 	else
   1145  1.1  christos 		return (ISC_FALSE);
   1146  1.1  christos }
   1147  1.1  christos 
   1148  1.1  christos 
   1149  1.1  christos /*
   1150  1.1  christos  * Compares only the public portion of two keys, by converting them
   1151  1.1  christos  * both to wire format and comparing the results.
   1152  1.1  christos  */
   1153  1.1  christos static isc_boolean_t
   1154  1.1  christos pub_compare(const dst_key_t *key1, const dst_key_t *key2) {
   1155  1.1  christos 	isc_result_t result;
   1156  1.1  christos 	unsigned char buf1[DST_KEY_MAXSIZE], buf2[DST_KEY_MAXSIZE];
   1157  1.1  christos 	isc_buffer_t b1, b2;
   1158  1.1  christos 	isc_region_t r1, r2;
   1159  1.1  christos 
   1160  1.1  christos 	isc_buffer_init(&b1, buf1, sizeof(buf1));
   1161  1.1  christos 	result = dst_key_todns(key1, &b1);
   1162  1.1  christos 	if (result != ISC_R_SUCCESS)
   1163  1.1  christos 		return (ISC_FALSE);
   1164  1.1  christos 	/* Zero out flags. */
   1165  1.1  christos 	buf1[0] = buf1[1] = 0;
   1166  1.1  christos 	if ((key1->key_flags & DNS_KEYFLAG_EXTENDED) != 0)
   1167  1.1  christos 		isc_buffer_subtract(&b1, 2);
   1168  1.1  christos 
   1169  1.1  christos 	isc_buffer_init(&b2, buf2, sizeof(buf2));
   1170  1.1  christos 	result = dst_key_todns(key2, &b2);
   1171  1.1  christos 	if (result != ISC_R_SUCCESS)
   1172  1.1  christos 		return (ISC_FALSE);
   1173  1.1  christos 	/* Zero out flags. */
   1174  1.1  christos 	buf2[0] = buf2[1] = 0;
   1175  1.1  christos 	if ((key2->key_flags & DNS_KEYFLAG_EXTENDED) != 0)
   1176  1.1  christos 		isc_buffer_subtract(&b2, 2);
   1177  1.1  christos 
   1178  1.1  christos 	isc_buffer_usedregion(&b1, &r1);
   1179  1.1  christos 	/* Remove extended flags. */
   1180  1.1  christos 	if ((key1->key_flags & DNS_KEYFLAG_EXTENDED) != 0) {
   1181  1.1  christos 		memmove(&buf1[4], &buf1[6], r1.length - 6);
   1182  1.1  christos 		r1.length -= 2;
   1183  1.1  christos 	}
   1184  1.1  christos 
   1185  1.1  christos 	isc_buffer_usedregion(&b2, &r2);
   1186  1.1  christos 	/* Remove extended flags. */
   1187  1.1  christos 	if ((key2->key_flags & DNS_KEYFLAG_EXTENDED) != 0) {
   1188  1.1  christos 		memmove(&buf2[4], &buf2[6], r2.length - 6);
   1189  1.1  christos 		r2.length -= 2;
   1190  1.1  christos 	}
   1191  1.1  christos 	return (ISC_TF(isc_region_compare(&r1, &r2) == 0));
   1192  1.1  christos }
   1193  1.1  christos 
   1194  1.1  christos isc_boolean_t
   1195  1.1  christos dst_key_compare(const dst_key_t *key1, const dst_key_t *key2) {
   1196  1.1  christos 	return (comparekeys(key1, key2, ISC_FALSE, key1->func->compare));
   1197  1.1  christos }
   1198  1.1  christos 
   1199  1.1  christos isc_boolean_t
   1200  1.1  christos dst_key_pubcompare(const dst_key_t *key1, const dst_key_t *key2,
   1201  1.1  christos 		   isc_boolean_t match_revoked_key)
   1202  1.1  christos {
   1203  1.1  christos 	return (comparekeys(key1, key2, match_revoked_key, pub_compare));
   1204  1.1  christos }
   1205  1.1  christos 
   1206  1.1  christos 
   1207  1.1  christos isc_boolean_t
   1208  1.1  christos dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2) {
   1209  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1210  1.1  christos 	REQUIRE(VALID_KEY(key1));
   1211  1.1  christos 	REQUIRE(VALID_KEY(key2));
   1212  1.1  christos 
   1213  1.1  christos 	if (key1 == key2)
   1214  1.1  christos 		return (ISC_TRUE);
   1215  1.1  christos 	if (key1->key_alg == key2->key_alg &&
   1216  1.1  christos 	    key1->func->paramcompare != NULL &&
   1217  1.1  christos 	    key1->func->paramcompare(key1, key2) == ISC_TRUE)
   1218  1.1  christos 		return (ISC_TRUE);
   1219  1.1  christos 	else
   1220  1.1  christos 		return (ISC_FALSE);
   1221  1.1  christos }
   1222  1.1  christos 
   1223  1.1  christos void
   1224  1.1  christos dst_key_attach(dst_key_t *source, dst_key_t **target) {
   1225  1.1  christos 
   1226  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1227  1.1  christos 	REQUIRE(target != NULL && *target == NULL);
   1228  1.1  christos 	REQUIRE(VALID_KEY(source));
   1229  1.1  christos 
   1230  1.1  christos 	isc_refcount_increment(&source->refs, NULL);
   1231  1.1  christos 	*target = source;
   1232  1.1  christos }
   1233  1.1  christos 
   1234  1.1  christos void
   1235  1.1  christos dst_key_free(dst_key_t **keyp) {
   1236  1.1  christos 	isc_mem_t *mctx;
   1237  1.1  christos 	dst_key_t *key;
   1238  1.1  christos 	unsigned int refs;
   1239  1.1  christos 
   1240  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1241  1.1  christos 	REQUIRE(keyp != NULL && VALID_KEY(*keyp));
   1242  1.1  christos 
   1243  1.1  christos 	key = *keyp;
   1244  1.1  christos 	mctx = key->mctx;
   1245  1.1  christos 
   1246  1.1  christos 	isc_refcount_decrement(&key->refs, &refs);
   1247  1.1  christos 	if (refs != 0)
   1248  1.1  christos 		return;
   1249  1.1  christos 
   1250  1.1  christos 	isc_refcount_destroy(&key->refs);
   1251  1.1  christos 	if (key->keydata.generic != NULL) {
   1252  1.1  christos 		INSIST(key->func->destroy != NULL);
   1253  1.1  christos 		key->func->destroy(key);
   1254  1.1  christos 	}
   1255  1.1  christos 	if (key->engine != NULL)
   1256  1.1  christos 		isc_mem_free(mctx, key->engine);
   1257  1.1  christos 	if (key->label != NULL)
   1258  1.1  christos 		isc_mem_free(mctx, key->label);
   1259  1.1  christos 	dns_name_free(key->key_name, mctx);
   1260  1.1  christos 	isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
   1261  1.1  christos 	if (key->key_tkeytoken) {
   1262  1.1  christos 		isc_buffer_free(&key->key_tkeytoken);
   1263  1.1  christos 	}
   1264  1.1  christos 	isc_safe_memwipe(key, sizeof(*key));
   1265  1.1  christos 	isc_mem_putanddetach(&mctx, key, sizeof(*key));
   1266  1.1  christos 	*keyp = NULL;
   1267  1.1  christos }
   1268  1.1  christos 
   1269  1.1  christos isc_boolean_t
   1270  1.1  christos dst_key_isprivate(const dst_key_t *key) {
   1271  1.1  christos 	REQUIRE(VALID_KEY(key));
   1272  1.1  christos 	INSIST(key->func->isprivate != NULL);
   1273  1.1  christos 	return (key->func->isprivate(key));
   1274  1.1  christos }
   1275  1.1  christos 
   1276  1.1  christos isc_result_t
   1277  1.1  christos dst_key_buildfilename(const dst_key_t *key, int type,
   1278  1.1  christos 		      const char *directory, isc_buffer_t *out) {
   1279  1.1  christos 
   1280  1.1  christos 	REQUIRE(VALID_KEY(key));
   1281  1.1  christos 	REQUIRE(type == DST_TYPE_PRIVATE || type == DST_TYPE_PUBLIC ||
   1282  1.1  christos 		type == 0);
   1283  1.1  christos 
   1284  1.1  christos 	return (buildfilename(key->key_name, key->key_id, key->key_alg,
   1285  1.1  christos 			      type, directory, out));
   1286  1.1  christos }
   1287  1.1  christos 
   1288  1.1  christos isc_result_t
   1289  1.1  christos dst_key_sigsize(const dst_key_t *key, unsigned int *n) {
   1290  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1291  1.1  christos 	REQUIRE(VALID_KEY(key));
   1292  1.1  christos 	REQUIRE(n != NULL);
   1293  1.1  christos 
   1294  1.1  christos 	/* XXXVIX this switch statement is too sparse to gen a jump table. */
   1295  1.1  christos 	switch (key->key_alg) {
   1296  1.1  christos #ifndef PK11_MD5_DISABLE
   1297  1.1  christos 	case DST_ALG_RSAMD5:
   1298  1.1  christos #endif
   1299  1.1  christos 	case DST_ALG_RSASHA1:
   1300  1.1  christos 	case DST_ALG_NSEC3RSASHA1:
   1301  1.1  christos 	case DST_ALG_RSASHA256:
   1302  1.1  christos 	case DST_ALG_RSASHA512:
   1303  1.1  christos 		*n = (key->key_size + 7) / 8;
   1304  1.1  christos 		break;
   1305  1.1  christos #ifndef PK11_DSA_DISABLE
   1306  1.1  christos 	case DST_ALG_DSA:
   1307  1.1  christos 	case DST_ALG_NSEC3DSA:
   1308  1.1  christos 		*n = DNS_SIG_DSASIGSIZE;
   1309  1.1  christos 		break;
   1310  1.1  christos #endif
   1311  1.1  christos 	case DST_ALG_ECCGOST:
   1312  1.1  christos 		*n = DNS_SIG_GOSTSIGSIZE;
   1313  1.1  christos 		break;
   1314  1.1  christos 	case DST_ALG_ECDSA256:
   1315  1.1  christos 		*n = DNS_SIG_ECDSA256SIZE;
   1316  1.1  christos 		break;
   1317  1.1  christos 	case DST_ALG_ECDSA384:
   1318  1.1  christos 		*n = DNS_SIG_ECDSA384SIZE;
   1319  1.1  christos 		break;
   1320  1.1  christos 	case DST_ALG_ED25519:
   1321  1.1  christos 		*n = DNS_SIG_ED25519SIZE;
   1322  1.1  christos 		break;
   1323  1.1  christos 	case DST_ALG_ED448:
   1324  1.1  christos 		*n = DNS_SIG_ED448SIZE;
   1325  1.1  christos 		break;
   1326  1.1  christos #ifndef PK11_MD5_DISABLE
   1327  1.1  christos 	case DST_ALG_HMACMD5:
   1328  1.1  christos 		*n = 16;
   1329  1.1  christos 		break;
   1330  1.1  christos #endif
   1331  1.1  christos 	case DST_ALG_HMACSHA1:
   1332  1.1  christos 		*n = ISC_SHA1_DIGESTLENGTH;
   1333  1.1  christos 		break;
   1334  1.1  christos 	case DST_ALG_HMACSHA224:
   1335  1.1  christos 		*n = ISC_SHA224_DIGESTLENGTH;
   1336  1.1  christos 		break;
   1337  1.1  christos 	case DST_ALG_HMACSHA256:
   1338  1.1  christos 		*n = ISC_SHA256_DIGESTLENGTH;
   1339  1.1  christos 		break;
   1340  1.1  christos 	case DST_ALG_HMACSHA384:
   1341  1.1  christos 		*n = ISC_SHA384_DIGESTLENGTH;
   1342  1.1  christos 		break;
   1343  1.1  christos 	case DST_ALG_HMACSHA512:
   1344  1.1  christos 		*n = ISC_SHA512_DIGESTLENGTH;
   1345  1.1  christos 		break;
   1346  1.1  christos 	case DST_ALG_GSSAPI:
   1347  1.1  christos 		*n = 128; /*%< XXX */
   1348  1.1  christos 		break;
   1349  1.1  christos #ifndef PK11_DH_DISABLE
   1350  1.1  christos 	case DST_ALG_DH:
   1351  1.1  christos #endif
   1352  1.1  christos 	default:
   1353  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
   1354  1.1  christos 	}
   1355  1.1  christos 	return (ISC_R_SUCCESS);
   1356  1.1  christos }
   1357  1.1  christos 
   1358  1.1  christos isc_result_t
   1359  1.1  christos dst_key_secretsize(const dst_key_t *key, unsigned int *n) {
   1360  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1361  1.1  christos 	REQUIRE(VALID_KEY(key));
   1362  1.1  christos 	REQUIRE(n != NULL);
   1363  1.1  christos 
   1364  1.1  christos #ifndef PK11_DH_DISABLE
   1365  1.1  christos 	if (key->key_alg == DST_ALG_DH)
   1366  1.1  christos 		*n = (key->key_size + 7) / 8;
   1367  1.1  christos 	else
   1368  1.1  christos #endif
   1369  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
   1370  1.1  christos #ifndef PK11_DH_DISABLE
   1371  1.1  christos 	return (ISC_R_SUCCESS);
   1372  1.1  christos #endif
   1373  1.1  christos }
   1374  1.1  christos 
   1375  1.1  christos /*%
   1376  1.1  christos  * Set the flags on a key, then recompute the key ID
   1377  1.1  christos  */
   1378  1.1  christos isc_result_t
   1379  1.1  christos dst_key_setflags(dst_key_t *key, isc_uint32_t flags) {
   1380  1.1  christos 	REQUIRE(VALID_KEY(key));
   1381  1.1  christos 	key->key_flags = flags;
   1382  1.1  christos 	return (computeid(key));
   1383  1.1  christos }
   1384  1.1  christos 
   1385  1.1  christos void
   1386  1.1  christos dst_key_format(const dst_key_t *key, char *cp, unsigned int size) {
   1387  1.1  christos 	char namestr[DNS_NAME_FORMATSIZE];
   1388  1.1  christos 	char algstr[DNS_NAME_FORMATSIZE];
   1389  1.1  christos 
   1390  1.1  christos 	dns_name_format(dst_key_name(key), namestr, sizeof(namestr));
   1391  1.1  christos 	dns_secalg_format((dns_secalg_t) dst_key_alg(key), algstr,
   1392  1.1  christos 			  sizeof(algstr));
   1393  1.1  christos 	snprintf(cp, size, "%s/%s/%d", namestr, algstr, dst_key_id(key));
   1394  1.1  christos }
   1395  1.1  christos 
   1396  1.1  christos isc_result_t
   1397  1.1  christos dst_key_dump(dst_key_t *key, isc_mem_t *mctx, char **buffer, int *length) {
   1398  1.1  christos 
   1399  1.1  christos 	REQUIRE(buffer != NULL && *buffer == NULL);
   1400  1.1  christos 	REQUIRE(length != NULL && *length == 0);
   1401  1.1  christos 	REQUIRE(VALID_KEY(key));
   1402  1.1  christos 
   1403  1.1  christos 	if (key->func->dump == NULL)
   1404  1.1  christos 		return (ISC_R_NOTIMPLEMENTED);
   1405  1.1  christos 	return (key->func->dump(key, mctx, buffer, length));
   1406  1.1  christos }
   1407  1.1  christos 
   1408  1.1  christos isc_result_t
   1409  1.1  christos dst_key_restore(dns_name_t *name, unsigned int alg, unsigned int flags,
   1410  1.1  christos 		unsigned int protocol, dns_rdataclass_t rdclass,
   1411  1.1  christos 		isc_mem_t *mctx, const char *keystr, dst_key_t **keyp)
   1412  1.1  christos {
   1413  1.1  christos 	isc_result_t result;
   1414  1.1  christos 	dst_key_t *key;
   1415  1.1  christos 
   1416  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1417  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
   1418  1.1  christos 
   1419  1.1  christos 	if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
   1420  1.1  christos 		return (DST_R_UNSUPPORTEDALG);
   1421  1.1  christos 
   1422  1.1  christos 	if (dst_t_func[alg]->restore == NULL)
   1423  1.1  christos 		return (ISC_R_NOTIMPLEMENTED);
   1424  1.1  christos 
   1425  1.1  christos 	key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
   1426  1.1  christos 	if (key == NULL)
   1427  1.1  christos 		return (ISC_R_NOMEMORY);
   1428  1.1  christos 
   1429  1.1  christos 	result = (dst_t_func[alg]->restore)(key, keystr);
   1430  1.1  christos 	if (result == ISC_R_SUCCESS)
   1431  1.1  christos 		*keyp = key;
   1432  1.1  christos 	else
   1433  1.1  christos 		dst_key_free(&key);
   1434  1.1  christos 
   1435  1.1  christos 	return (result);
   1436  1.1  christos }
   1437  1.1  christos 
   1438  1.1  christos /***
   1439  1.1  christos  *** Static methods
   1440  1.1  christos  ***/
   1441  1.1  christos 
   1442  1.1  christos /*%
   1443  1.1  christos  * Allocates a key structure and fills in some of the fields.
   1444  1.1  christos  */
   1445  1.1  christos static dst_key_t *
   1446  1.1  christos get_key_struct(const dns_name_t *name, unsigned int alg,
   1447  1.1  christos 	       unsigned int flags, unsigned int protocol,
   1448  1.1  christos 	       unsigned int bits, dns_rdataclass_t rdclass,
   1449  1.1  christos 	       dns_ttl_t ttl, isc_mem_t *mctx)
   1450  1.1  christos {
   1451  1.1  christos 	dst_key_t *key;
   1452  1.1  christos 	isc_result_t result;
   1453  1.1  christos 	int i;
   1454  1.1  christos 
   1455  1.1  christos 	key = (dst_key_t *) isc_mem_get(mctx, sizeof(dst_key_t));
   1456  1.1  christos 	if (key == NULL)
   1457  1.1  christos 		return (NULL);
   1458  1.1  christos 
   1459  1.1  christos 	memset(key, 0, sizeof(dst_key_t));
   1460  1.1  christos 
   1461  1.1  christos 	key->key_name = isc_mem_get(mctx, sizeof(dns_name_t));
   1462  1.1  christos 	if (key->key_name == NULL) {
   1463  1.1  christos 		isc_mem_put(mctx, key, sizeof(dst_key_t));
   1464  1.1  christos 		return (NULL);
   1465  1.1  christos 	}
   1466  1.1  christos 
   1467  1.1  christos 	dns_name_init(key->key_name, NULL);
   1468  1.1  christos 	result = dns_name_dup(name, mctx, key->key_name);
   1469  1.1  christos 	if (result != ISC_R_SUCCESS) {
   1470  1.1  christos 		isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
   1471  1.1  christos 		isc_mem_put(mctx, key, sizeof(dst_key_t));
   1472  1.1  christos 		return (NULL);
   1473  1.1  christos 	}
   1474  1.1  christos 
   1475  1.1  christos 	result = isc_refcount_init(&key->refs, 1);
   1476  1.1  christos 	if (result != ISC_R_SUCCESS) {
   1477  1.1  christos 		dns_name_free(key->key_name, mctx);
   1478  1.1  christos 		isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
   1479  1.1  christos 		isc_mem_put(mctx, key, sizeof(dst_key_t));
   1480  1.1  christos 		return (NULL);
   1481  1.1  christos 	}
   1482  1.1  christos 	isc_mem_attach(mctx, &key->mctx);
   1483  1.1  christos 	key->key_alg = alg;
   1484  1.1  christos 	key->key_flags = flags;
   1485  1.1  christos 	key->key_proto = protocol;
   1486  1.1  christos 	key->keydata.generic = NULL;
   1487  1.1  christos 	key->key_size = bits;
   1488  1.1  christos 	key->key_class = rdclass;
   1489  1.1  christos 	key->key_ttl = ttl;
   1490  1.1  christos 	key->func = dst_t_func[alg];
   1491  1.1  christos 	key->fmt_major = 0;
   1492  1.1  christos 	key->fmt_minor = 0;
   1493  1.1  christos 	for (i = 0; i < (DST_MAX_TIMES + 1); i++) {
   1494  1.1  christos 		key->times[i] = 0;
   1495  1.1  christos 		key->timeset[i] = ISC_FALSE;
   1496  1.1  christos 	}
   1497  1.1  christos 	key->inactive = ISC_FALSE;
   1498  1.1  christos 	key->magic = KEY_MAGIC;
   1499  1.1  christos 	return (key);
   1500  1.1  christos }
   1501  1.1  christos 
   1502  1.1  christos isc_boolean_t
   1503  1.1  christos dst_key_inactive(const dst_key_t *key) {
   1504  1.1  christos 
   1505  1.1  christos 	REQUIRE(VALID_KEY(key));
   1506  1.1  christos 
   1507  1.1  christos 	return (key->inactive);
   1508  1.1  christos }
   1509  1.1  christos 
   1510  1.1  christos void
   1511  1.1  christos dst_key_setinactive(dst_key_t *key, isc_boolean_t inactive) {
   1512  1.1  christos 
   1513  1.1  christos 	REQUIRE(VALID_KEY(key));
   1514  1.1  christos 
   1515  1.1  christos 	key->inactive = inactive;
   1516  1.1  christos }
   1517  1.1  christos 
   1518  1.1  christos /*%
   1519  1.1  christos  * Reads a public key from disk
   1520  1.1  christos  */
   1521  1.1  christos isc_result_t
   1522  1.1  christos dst_key_read_public(const char *filename, int type,
   1523  1.1  christos 		    isc_mem_t *mctx, dst_key_t **keyp)
   1524  1.1  christos {
   1525  1.1  christos 	u_char rdatabuf[DST_KEY_MAXSIZE];
   1526  1.1  christos 	isc_buffer_t b;
   1527  1.1  christos 	dns_fixedname_t name;
   1528  1.1  christos 	isc_lex_t *lex = NULL;
   1529  1.1  christos 	isc_token_t token;
   1530  1.1  christos 	isc_result_t ret;
   1531  1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
   1532  1.1  christos 	unsigned int opt = ISC_LEXOPT_DNSMULTILINE;
   1533  1.1  christos 	dns_rdataclass_t rdclass = dns_rdataclass_in;
   1534  1.1  christos 	isc_lexspecials_t specials;
   1535  1.1  christos 	isc_uint32_t ttl = 0;
   1536  1.1  christos 	isc_result_t result;
   1537  1.1  christos 	dns_rdatatype_t keytype;
   1538  1.1  christos 
   1539  1.1  christos 	/*
   1540  1.1  christos 	 * Open the file and read its formatted contents
   1541  1.1  christos 	 * File format:
   1542  1.1  christos 	 *    domain.name [ttl] [class] [KEY|DNSKEY] <flags> <protocol> <algorithm> <key>
   1543  1.1  christos 	 */
   1544  1.1  christos 
   1545  1.1  christos 	/* 1500 should be large enough for any key */
   1546  1.1  christos 	ret = isc_lex_create(mctx, 1500, &lex);
   1547  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1548  1.1  christos 		goto cleanup;
   1549  1.1  christos 
   1550  1.1  christos 	memset(specials, 0, sizeof(specials));
   1551  1.1  christos 	specials['('] = 1;
   1552  1.1  christos 	specials[')'] = 1;
   1553  1.1  christos 	specials['"'] = 1;
   1554  1.1  christos 	isc_lex_setspecials(lex, specials);
   1555  1.1  christos 	isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
   1556  1.1  christos 
   1557  1.1  christos 	ret = isc_lex_openfile(lex, filename);
   1558  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1559  1.1  christos 		goto cleanup;
   1560  1.1  christos 
   1561  1.1  christos #define NEXTTOKEN(lex, opt, token) { \
   1562  1.1  christos 	ret = isc_lex_gettoken(lex, opt, token); \
   1563  1.1  christos 	if (ret != ISC_R_SUCCESS) \
   1564  1.1  christos 		goto cleanup; \
   1565  1.1  christos 	}
   1566  1.1  christos 
   1567  1.1  christos #define BADTOKEN() { \
   1568  1.1  christos 	ret = ISC_R_UNEXPECTEDTOKEN; \
   1569  1.1  christos 	goto cleanup; \
   1570  1.1  christos 	}
   1571  1.1  christos 
   1572  1.1  christos 	/* Read the domain name */
   1573  1.1  christos 	NEXTTOKEN(lex, opt, &token);
   1574  1.1  christos 	if (token.type != isc_tokentype_string)
   1575  1.1  christos 		BADTOKEN();
   1576  1.1  christos 
   1577  1.1  christos 	/*
   1578  1.1  christos 	 * We don't support "@" in .key files.
   1579  1.1  christos 	 */
   1580  1.1  christos 	if (!strcmp(DST_AS_STR(token), "@"))
   1581  1.1  christos 		BADTOKEN();
   1582  1.1  christos 
   1583  1.1  christos 	dns_fixedname_init(&name);
   1584  1.1  christos 	isc_buffer_init(&b, DST_AS_STR(token), strlen(DST_AS_STR(token)));
   1585  1.1  christos 	isc_buffer_add(&b, strlen(DST_AS_STR(token)));
   1586  1.1  christos 	ret = dns_name_fromtext(dns_fixedname_name(&name), &b, dns_rootname,
   1587  1.1  christos 				0, NULL);
   1588  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1589  1.1  christos 		goto cleanup;
   1590  1.1  christos 
   1591  1.1  christos 	/* Read the next word: either TTL, class, or 'KEY' */
   1592  1.1  christos 	NEXTTOKEN(lex, opt, &token);
   1593  1.1  christos 
   1594  1.1  christos 	if (token.type != isc_tokentype_string)
   1595  1.1  christos 		BADTOKEN();
   1596  1.1  christos 
   1597  1.1  christos 	/* If it's a TTL, read the next one */
   1598  1.1  christos 	result = dns_ttl_fromtext(&token.value.as_textregion, &ttl);
   1599  1.1  christos 	if (result == ISC_R_SUCCESS)
   1600  1.1  christos 		NEXTTOKEN(lex, opt, &token);
   1601  1.1  christos 
   1602  1.1  christos 	if (token.type != isc_tokentype_string)
   1603  1.1  christos 		BADTOKEN();
   1604  1.1  christos 
   1605  1.1  christos 	ret = dns_rdataclass_fromtext(&rdclass, &token.value.as_textregion);
   1606  1.1  christos 	if (ret == ISC_R_SUCCESS)
   1607  1.1  christos 		NEXTTOKEN(lex, opt, &token);
   1608  1.1  christos 
   1609  1.1  christos 	if (token.type != isc_tokentype_string)
   1610  1.1  christos 		BADTOKEN();
   1611  1.1  christos 
   1612  1.1  christos 	if (strcasecmp(DST_AS_STR(token), "DNSKEY") == 0)
   1613  1.1  christos 		keytype = dns_rdatatype_dnskey;
   1614  1.1  christos 	else if (strcasecmp(DST_AS_STR(token), "KEY") == 0)
   1615  1.1  christos 		keytype = dns_rdatatype_key; /*%< SIG(0), TKEY */
   1616  1.1  christos 	else
   1617  1.1  christos 		BADTOKEN();
   1618  1.1  christos 
   1619  1.1  christos 	if (((type & DST_TYPE_KEY) != 0 && keytype != dns_rdatatype_key) ||
   1620  1.1  christos 	    ((type & DST_TYPE_KEY) == 0 && keytype != dns_rdatatype_dnskey)) {
   1621  1.1  christos 		ret = DST_R_BADKEYTYPE;
   1622  1.1  christos 		goto cleanup;
   1623  1.1  christos 	}
   1624  1.1  christos 
   1625  1.1  christos 	isc_buffer_init(&b, rdatabuf, sizeof(rdatabuf));
   1626  1.1  christos 	ret = dns_rdata_fromtext(&rdata, rdclass, keytype, lex, NULL,
   1627  1.1  christos 				 ISC_FALSE, mctx, &b, NULL);
   1628  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1629  1.1  christos 		goto cleanup;
   1630  1.1  christos 
   1631  1.1  christos 	ret = dst_key_fromdns(dns_fixedname_name(&name), rdclass, &b, mctx,
   1632  1.1  christos 			      keyp);
   1633  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1634  1.1  christos 		goto cleanup;
   1635  1.1  christos 
   1636  1.1  christos 	dst_key_setttl(*keyp, ttl);
   1637  1.1  christos 
   1638  1.1  christos  cleanup:
   1639  1.1  christos 	if (lex != NULL)
   1640  1.1  christos 		isc_lex_destroy(&lex);
   1641  1.1  christos 	return (ret);
   1642  1.1  christos }
   1643  1.1  christos 
   1644  1.1  christos static isc_boolean_t
   1645  1.1  christos issymmetric(const dst_key_t *key) {
   1646  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1647  1.1  christos 	REQUIRE(VALID_KEY(key));
   1648  1.1  christos 
   1649  1.1  christos 	/* XXXVIX this switch statement is too sparse to gen a jump table. */
   1650  1.1  christos 	switch (key->key_alg) {
   1651  1.1  christos #ifndef PK11_MD5_DISABLE
   1652  1.1  christos 	case DST_ALG_RSAMD5:
   1653  1.1  christos #endif
   1654  1.1  christos 	case DST_ALG_RSASHA1:
   1655  1.1  christos 	case DST_ALG_NSEC3RSASHA1:
   1656  1.1  christos 	case DST_ALG_RSASHA256:
   1657  1.1  christos 	case DST_ALG_RSASHA512:
   1658  1.1  christos #ifndef PK11_DSA_DISABLE
   1659  1.1  christos 	case DST_ALG_DSA:
   1660  1.1  christos 	case DST_ALG_NSEC3DSA:
   1661  1.1  christos #endif
   1662  1.1  christos #ifndef PK11_DH_DISABLE
   1663  1.1  christos 	case DST_ALG_DH:
   1664  1.1  christos #endif
   1665  1.1  christos 	case DST_ALG_ECCGOST:
   1666  1.1  christos 	case DST_ALG_ECDSA256:
   1667  1.1  christos 	case DST_ALG_ECDSA384:
   1668  1.1  christos 	case DST_ALG_ED25519:
   1669  1.1  christos 	case DST_ALG_ED448:
   1670  1.1  christos 		return (ISC_FALSE);
   1671  1.1  christos #ifndef PK11_MD5_DISABLE
   1672  1.1  christos 	case DST_ALG_HMACMD5:
   1673  1.1  christos #endif
   1674  1.1  christos 	case DST_ALG_HMACSHA1:
   1675  1.1  christos 	case DST_ALG_HMACSHA224:
   1676  1.1  christos 	case DST_ALG_HMACSHA256:
   1677  1.1  christos 	case DST_ALG_HMACSHA384:
   1678  1.1  christos 	case DST_ALG_HMACSHA512:
   1679  1.1  christos 	case DST_ALG_GSSAPI:
   1680  1.1  christos 		return (ISC_TRUE);
   1681  1.1  christos 	default:
   1682  1.1  christos 		return (ISC_FALSE);
   1683  1.1  christos 	}
   1684  1.1  christos }
   1685  1.1  christos 
   1686  1.1  christos /*%
   1687  1.1  christos  * Write key timing metadata to a file pointer, preceded by 'tag'
   1688  1.1  christos  */
   1689  1.1  christos static void
   1690  1.1  christos printtime(const dst_key_t *key, int type, const char *tag, FILE *stream) {
   1691  1.1  christos 	isc_result_t result;
   1692  1.1  christos #ifdef ISC_PLATFORM_USETHREADS
   1693  1.1  christos 	char output[26]; /* Minimum buffer as per ctime_r() specification. */
   1694  1.1  christos #else
   1695  1.1  christos 	const char *output;
   1696  1.1  christos #endif
   1697  1.1  christos 	isc_stdtime_t when;
   1698  1.1  christos 	time_t t;
   1699  1.1  christos 	char utc[sizeof("YYYYMMDDHHSSMM")];
   1700  1.1  christos 	isc_buffer_t b;
   1701  1.1  christos 	isc_region_t r;
   1702  1.1  christos 
   1703  1.1  christos 	result = dst_key_gettime(key, type, &when);
   1704  1.1  christos 	if (result == ISC_R_NOTFOUND)
   1705  1.1  christos 		return;
   1706  1.1  christos 
   1707  1.1  christos 	/* time_t and isc_stdtime_t might be different sizes */
   1708  1.1  christos 	t = when;
   1709  1.1  christos #ifdef ISC_PLATFORM_USETHREADS
   1710  1.1  christos #ifdef WIN32
   1711  1.1  christos 	if (ctime_s(output, sizeof(output), &t) != 0)
   1712  1.1  christos 		goto error;
   1713  1.1  christos #else
   1714  1.1  christos 	if (ctime_r(&t, output) == NULL)
   1715  1.1  christos 		goto error;
   1716  1.1  christos #endif
   1717  1.1  christos #else
   1718  1.1  christos 	output = ctime(&t);
   1719  1.1  christos #endif
   1720  1.1  christos 
   1721  1.1  christos 	isc_buffer_init(&b, utc, sizeof(utc));
   1722  1.1  christos 	result = dns_time32_totext(when, &b);
   1723  1.1  christos 	if (result != ISC_R_SUCCESS)
   1724  1.1  christos 		goto error;
   1725  1.1  christos 
   1726  1.1  christos 	isc_buffer_usedregion(&b, &r);
   1727  1.1  christos 	fprintf(stream, "%s: %.*s (%.*s)\n", tag, (int)r.length, r.base,
   1728  1.1  christos 		 (int)strlen(output) - 1, output);
   1729  1.1  christos 	return;
   1730  1.1  christos 
   1731  1.1  christos  error:
   1732  1.1  christos 	fprintf(stream, "%s: (set, unable to display)\n", tag);
   1733  1.1  christos }
   1734  1.1  christos 
   1735  1.1  christos /*%
   1736  1.1  christos  * Writes a public key to disk in DNS format.
   1737  1.1  christos  */
   1738  1.1  christos static isc_result_t
   1739  1.1  christos write_public_key(const dst_key_t *key, int type, const char *directory) {
   1740  1.1  christos 	FILE *fp;
   1741  1.1  christos 	isc_buffer_t keyb, textb, fileb, classb;
   1742  1.1  christos 	isc_region_t r;
   1743  1.1  christos 	char filename[ISC_DIR_NAMEMAX];
   1744  1.1  christos 	unsigned char key_array[DST_KEY_MAXSIZE];
   1745  1.1  christos 	char text_array[DST_KEY_MAXTEXTSIZE];
   1746  1.1  christos 	char class_array[10];
   1747  1.1  christos 	isc_result_t ret;
   1748  1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
   1749  1.1  christos 	isc_fsaccess_t access;
   1750  1.1  christos 
   1751  1.1  christos 	REQUIRE(VALID_KEY(key));
   1752  1.1  christos 
   1753  1.1  christos 	isc_buffer_init(&keyb, key_array, sizeof(key_array));
   1754  1.1  christos 	isc_buffer_init(&textb, text_array, sizeof(text_array));
   1755  1.1  christos 	isc_buffer_init(&classb, class_array, sizeof(class_array));
   1756  1.1  christos 
   1757  1.1  christos 	ret = dst_key_todns(key, &keyb);
   1758  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1759  1.1  christos 		return (ret);
   1760  1.1  christos 
   1761  1.1  christos 	isc_buffer_usedregion(&keyb, &r);
   1762  1.1  christos 	dns_rdata_fromregion(&rdata, key->key_class, dns_rdatatype_dnskey, &r);
   1763  1.1  christos 
   1764  1.1  christos 	ret = dns_rdata_totext(&rdata, (dns_name_t *) NULL, &textb);
   1765  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1766  1.1  christos 		return (DST_R_INVALIDPUBLICKEY);
   1767  1.1  christos 
   1768  1.1  christos 	ret = dns_rdataclass_totext(key->key_class, &classb);
   1769  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1770  1.1  christos 		return (DST_R_INVALIDPUBLICKEY);
   1771  1.1  christos 
   1772  1.1  christos 	/*
   1773  1.1  christos 	 * Make the filename.
   1774  1.1  christos 	 */
   1775  1.1  christos 	isc_buffer_init(&fileb, filename, sizeof(filename));
   1776  1.1  christos 	ret = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory, &fileb);
   1777  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1778  1.1  christos 		return (ret);
   1779  1.1  christos 
   1780  1.1  christos 	/*
   1781  1.1  christos 	 * Create public key file.
   1782  1.1  christos 	 */
   1783  1.1  christos 	if ((fp = fopen(filename, "w")) == NULL)
   1784  1.1  christos 		return (DST_R_WRITEERROR);
   1785  1.1  christos 
   1786  1.1  christos 	if (issymmetric(key)) {
   1787  1.1  christos 		access = 0;
   1788  1.1  christos 		isc_fsaccess_add(ISC_FSACCESS_OWNER,
   1789  1.1  christos 				 ISC_FSACCESS_READ | ISC_FSACCESS_WRITE,
   1790  1.1  christos 				 &access);
   1791  1.1  christos 		(void)isc_fsaccess_set(filename, access);
   1792  1.1  christos 	}
   1793  1.1  christos 
   1794  1.1  christos 	/* Write key information in comments */
   1795  1.1  christos 	if ((type & DST_TYPE_KEY) == 0) {
   1796  1.1  christos 		fprintf(fp, "; This is a %s%s-signing key, keyid %d, for ",
   1797  1.1  christos 			(key->key_flags & DNS_KEYFLAG_REVOKE) != 0 ?
   1798  1.1  christos 				"revoked " :
   1799  1.1  christos 				"",
   1800  1.1  christos 			(key->key_flags & DNS_KEYFLAG_KSK) != 0 ?
   1801  1.1  christos 				"key" :
   1802  1.1  christos 				"zone",
   1803  1.1  christos 			key->key_id);
   1804  1.1  christos 		ret = dns_name_print(key->key_name, fp);
   1805  1.1  christos 		if (ret != ISC_R_SUCCESS) {
   1806  1.1  christos 			fclose(fp);
   1807  1.1  christos 			return (ret);
   1808  1.1  christos 		}
   1809  1.1  christos 		fputc('\n', fp);
   1810  1.1  christos 
   1811  1.1  christos 		printtime(key, DST_TIME_CREATED, "; Created", fp);
   1812  1.1  christos 		printtime(key, DST_TIME_PUBLISH, "; Publish", fp);
   1813  1.1  christos 		printtime(key, DST_TIME_ACTIVATE, "; Activate", fp);
   1814  1.1  christos 		printtime(key, DST_TIME_REVOKE, "; Revoke", fp);
   1815  1.1  christos 		printtime(key, DST_TIME_INACTIVE, "; Inactive", fp);
   1816  1.1  christos 		printtime(key, DST_TIME_DELETE, "; Delete", fp);
   1817  1.1  christos 		printtime(key, DST_TIME_SYNCPUBLISH , "; SyncPublish", fp);
   1818  1.1  christos 		printtime(key, DST_TIME_SYNCDELETE , "; SyncDelete", fp);
   1819  1.1  christos 	}
   1820  1.1  christos 
   1821  1.1  christos 	/* Now print the actual key */
   1822  1.1  christos 	ret = dns_name_print(key->key_name, fp);
   1823  1.1  christos 	fprintf(fp, " ");
   1824  1.1  christos 
   1825  1.1  christos 	if (key->key_ttl != 0)
   1826  1.1  christos 		fprintf(fp, "%u ", key->key_ttl);
   1827  1.1  christos 
   1828  1.1  christos 	isc_buffer_usedregion(&classb, &r);
   1829  1.1  christos 	if ((unsigned) fwrite(r.base, 1, r.length, fp) != r.length)
   1830  1.1  christos 	       ret = DST_R_WRITEERROR;
   1831  1.1  christos 
   1832  1.1  christos 	if ((type & DST_TYPE_KEY) != 0)
   1833  1.1  christos 		fprintf(fp, " KEY ");
   1834  1.1  christos 	else
   1835  1.1  christos 		fprintf(fp, " DNSKEY ");
   1836  1.1  christos 
   1837  1.1  christos 	isc_buffer_usedregion(&textb, &r);
   1838  1.1  christos 	if ((unsigned) fwrite(r.base, 1, r.length, fp) != r.length)
   1839  1.1  christos 	       ret = DST_R_WRITEERROR;
   1840  1.1  christos 
   1841  1.1  christos 	fputc('\n', fp);
   1842  1.1  christos 	fflush(fp);
   1843  1.1  christos 	if (ferror(fp))
   1844  1.1  christos 		ret = DST_R_WRITEERROR;
   1845  1.1  christos 	fclose(fp);
   1846  1.1  christos 
   1847  1.1  christos 	return (ret);
   1848  1.1  christos }
   1849  1.1  christos 
   1850  1.1  christos static isc_result_t
   1851  1.1  christos buildfilename(dns_name_t *name, dns_keytag_t id,
   1852  1.1  christos 	      unsigned int alg, unsigned int type,
   1853  1.1  christos 	      const char *directory, isc_buffer_t *out)
   1854  1.1  christos {
   1855  1.1  christos 	const char *suffix = "";
   1856  1.1  christos 	isc_result_t result;
   1857  1.1  christos 
   1858  1.1  christos 	REQUIRE(out != NULL);
   1859  1.1  christos 	if ((type & DST_TYPE_PRIVATE) != 0)
   1860  1.1  christos 		suffix = ".private";
   1861  1.1  christos 	else if (type == DST_TYPE_PUBLIC)
   1862  1.1  christos 		suffix = ".key";
   1863  1.1  christos 	if (directory != NULL) {
   1864  1.1  christos 		if (isc_buffer_availablelength(out) < strlen(directory))
   1865  1.1  christos 			return (ISC_R_NOSPACE);
   1866  1.1  christos 		isc_buffer_putstr(out, directory);
   1867  1.1  christos 		if (strlen(directory) > 0U &&
   1868  1.1  christos 		    directory[strlen(directory) - 1] != '/')
   1869  1.1  christos 			isc_buffer_putstr(out, "/");
   1870  1.1  christos 	}
   1871  1.1  christos 	if (isc_buffer_availablelength(out) < 1)
   1872  1.1  christos 		return (ISC_R_NOSPACE);
   1873  1.1  christos 	isc_buffer_putstr(out, "K");
   1874  1.1  christos 	result = dns_name_tofilenametext(name, ISC_FALSE, out);
   1875  1.1  christos 	if (result != ISC_R_SUCCESS)
   1876  1.1  christos 		return (result);
   1877  1.1  christos 
   1878  1.1  christos 	return (isc_buffer_printf(out, "+%03d+%05d%s", alg, id, suffix));
   1879  1.1  christos }
   1880  1.1  christos 
   1881  1.1  christos static isc_result_t
   1882  1.1  christos computeid(dst_key_t *key) {
   1883  1.1  christos 	isc_buffer_t dnsbuf;
   1884  1.1  christos 	unsigned char dns_array[DST_KEY_MAXSIZE];
   1885  1.1  christos 	isc_region_t r;
   1886  1.1  christos 	isc_result_t ret;
   1887  1.1  christos 
   1888  1.1  christos 	isc_buffer_init(&dnsbuf, dns_array, sizeof(dns_array));
   1889  1.1  christos 	ret = dst_key_todns(key, &dnsbuf);
   1890  1.1  christos 	if (ret != ISC_R_SUCCESS)
   1891  1.1  christos 		return (ret);
   1892  1.1  christos 
   1893  1.1  christos 	isc_buffer_usedregion(&dnsbuf, &r);
   1894  1.1  christos 	key->key_id = dst_region_computeid(&r, key->key_alg);
   1895  1.1  christos 	key->key_rid = dst_region_computerid(&r, key->key_alg);
   1896  1.1  christos 	return (ISC_R_SUCCESS);
   1897  1.1  christos }
   1898  1.1  christos 
   1899  1.1  christos static isc_result_t
   1900  1.1  christos frombuffer(const dns_name_t *name, unsigned int alg, unsigned int flags,
   1901  1.1  christos 	   unsigned int protocol, dns_rdataclass_t rdclass,
   1902  1.1  christos 	   isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
   1903  1.1  christos {
   1904  1.1  christos 	dst_key_t *key;
   1905  1.1  christos 	isc_result_t ret;
   1906  1.1  christos 
   1907  1.1  christos 	REQUIRE(dns_name_isabsolute(name));
   1908  1.1  christos 	REQUIRE(source != NULL);
   1909  1.1  christos 	REQUIRE(mctx != NULL);
   1910  1.1  christos 	REQUIRE(keyp != NULL && *keyp == NULL);
   1911  1.1  christos 
   1912  1.1  christos 	key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
   1913  1.1  christos 	if (key == NULL)
   1914  1.1  christos 		return (ISC_R_NOMEMORY);
   1915  1.1  christos 
   1916  1.1  christos 	if (isc_buffer_remaininglength(source) > 0) {
   1917  1.1  christos 		ret = algorithm_status(alg);
   1918  1.1  christos 		if (ret != ISC_R_SUCCESS) {
   1919  1.1  christos 			dst_key_free(&key);
   1920  1.1  christos 			return (ret);
   1921  1.1  christos 		}
   1922  1.1  christos 		if (key->func->fromdns == NULL) {
   1923  1.1  christos 			dst_key_free(&key);
   1924  1.1  christos 			return (DST_R_UNSUPPORTEDALG);
   1925  1.1  christos 		}
   1926  1.1  christos 
   1927  1.1  christos 		ret = key->func->fromdns(key, source);
   1928  1.1  christos 		if (ret != ISC_R_SUCCESS) {
   1929  1.1  christos 			dst_key_free(&key);
   1930  1.1  christos 			return (ret);
   1931  1.1  christos 		}
   1932  1.1  christos 	}
   1933  1.1  christos 
   1934  1.1  christos 	*keyp = key;
   1935  1.1  christos 	return (ISC_R_SUCCESS);
   1936  1.1  christos }
   1937  1.1  christos 
   1938  1.1  christos static isc_result_t
   1939  1.1  christos algorithm_status(unsigned int alg) {
   1940  1.1  christos 	REQUIRE(dst_initialized == ISC_TRUE);
   1941  1.1  christos 
   1942  1.1  christos 	if (dst_algorithm_supported(alg))
   1943  1.1  christos 		return (ISC_R_SUCCESS);
   1944  1.1  christos #if !defined(OPENSSL) && !defined(PKCS11CRYPTO)
   1945  1.1  christos 	if (alg == DST_ALG_RSAMD5 || alg == DST_ALG_RSASHA1 ||
   1946  1.1  christos 	    alg == DST_ALG_DSA || alg == DST_ALG_DH ||
   1947  1.1  christos 	    alg == DST_ALG_HMACMD5 || alg == DST_ALG_NSEC3DSA ||
   1948  1.1  christos 	    alg == DST_ALG_NSEC3RSASHA1 ||
   1949  1.1  christos 	    alg == DST_ALG_RSASHA256 || alg == DST_ALG_RSASHA512 ||
   1950  1.1  christos 	    alg == DST_ALG_ECCGOST ||
   1951  1.1  christos 	    alg == DST_ALG_ECDSA256 || alg == DST_ALG_ECDSA384 ||
   1952  1.1  christos 	    alg == DST_ALG_ED25519 || alg == DST_ALG_ED448)
   1953  1.1  christos 		return (DST_R_NOCRYPTO);
   1954  1.1  christos #endif
   1955  1.1  christos 	return (DST_R_UNSUPPORTEDALG);
   1956  1.1  christos }
   1957  1.1  christos 
   1958  1.1  christos static isc_result_t
   1959  1.1  christos addsuffix(char *filename, int len, const char *odirname,
   1960  1.1  christos 	  const char *ofilename, const char *suffix)
   1961  1.1  christos {
   1962  1.1  christos 	int olen = strlen(ofilename);
   1963  1.1  christos 	int n;
   1964  1.1  christos 
   1965  1.1  christos 	if (olen > 1 && ofilename[olen - 1] == '.')
   1966  1.1  christos 		olen -= 1;
   1967  1.1  christos 	else if (olen > 8 && strcmp(ofilename + olen - 8, ".private") == 0)
   1968  1.1  christos 		olen -= 8;
   1969  1.1  christos 	else if (olen > 4 && strcmp(ofilename + olen - 4, ".key") == 0)
   1970  1.1  christos 		olen -= 4;
   1971  1.1  christos 
   1972  1.1  christos 	if (odirname == NULL)
   1973  1.1  christos 		n = snprintf(filename, len, "%.*s%s", olen, ofilename, suffix);
   1974  1.1  christos 	else
   1975  1.1  christos 		n = snprintf(filename, len, "%s/%.*s%s",
   1976  1.1  christos 			     odirname, olen, ofilename, suffix);
   1977  1.1  christos 	if (n < 0)
   1978  1.1  christos 		return (ISC_R_FAILURE);
   1979  1.1  christos 	if (n >= len)
   1980  1.1  christos 		return (ISC_R_NOSPACE);
   1981  1.1  christos 	return (ISC_R_SUCCESS);
   1982  1.1  christos }
   1983  1.1  christos 
   1984  1.1  christos isc_result_t
   1985  1.1  christos dst__entropy_getdata(void *buf, unsigned int len, isc_boolean_t pseudo) {
   1986  1.1  christos 	unsigned int flags = dst_entropy_flags;
   1987  1.1  christos 
   1988  1.1  christos 	if (dst_entropy_pool == NULL)
   1989  1.1  christos 		return (ISC_R_FAILURE);
   1990  1.1  christos 
   1991  1.1  christos 	if (len == 0)
   1992  1.1  christos 		return (ISC_R_SUCCESS);
   1993  1.1  christos 
   1994  1.1  christos #ifdef PKCS11CRYPTO
   1995  1.1  christos 	UNUSED(pseudo);
   1996  1.1  christos 	UNUSED(flags);
   1997  1.1  christos 	return (pk11_rand_bytes(buf, len));
   1998  1.1  christos #else /* PKCS11CRYPTO */
   1999  1.1  christos 	if (pseudo)
   2000  1.1  christos 		flags &= ~ISC_ENTROPY_GOODONLY;
   2001  1.1  christos 	else
   2002  1.1  christos 		flags |= ISC_ENTROPY_BLOCKING;
   2003  1.1  christos #ifdef ISC_PLATFORM_CRYPTORANDOM
   2004  1.1  christos 	/* get entropy directly from crypto provider */
   2005  1.1  christos 	return (dst_random_getdata(buf, len, NULL, flags));
   2006  1.1  christos #else
   2007  1.1  christos 	/* get entropy from entropy source or hook function */
   2008  1.1  christos 	return (isc_entropy_getdata(dst_entropy_pool, buf, len, NULL, flags));
   2009  1.1  christos #endif /* ISC_PLATFORM_CRYPTORANDOM */
   2010  1.1  christos #endif /* PKCS11CRYPTO */
   2011  1.1  christos }
   2012  1.1  christos 
   2013  1.1  christos unsigned int
   2014  1.1  christos dst__entropy_status(void) {
   2015  1.1  christos #if !defined(PKCS11CRYPTO) && !defined(ISC_PLATFORM_CRYPTORANDOM)
   2016  1.1  christos #ifdef GSSAPI
   2017  1.1  christos 	unsigned int flags = dst_entropy_flags;
   2018  1.1  christos 	isc_result_t ret;
   2019  1.1  christos 	unsigned char buf[32];
   2020  1.1  christos 	static isc_boolean_t first = ISC_TRUE;
   2021  1.1  christos 
   2022  1.1  christos 	if (dst_entropy_pool == NULL)
   2023  1.1  christos 		return (0);
   2024  1.1  christos 
   2025  1.1  christos 	if (first) {
   2026  1.1  christos 		/* Someone believes RAND_status() initializes the PRNG */
   2027  1.1  christos 		flags &= ~ISC_ENTROPY_GOODONLY;
   2028  1.1  christos 		ret = isc_entropy_getdata(dst_entropy_pool, buf,
   2029  1.1  christos 					  sizeof(buf), NULL, flags);
   2030  1.1  christos 		INSIST(ret == ISC_R_SUCCESS);
   2031  1.1  christos 		isc_entropy_putdata(dst_entropy_pool, buf,
   2032  1.1  christos 				    sizeof(buf), 2 * sizeof(buf));
   2033  1.1  christos 		first = ISC_FALSE;
   2034  1.1  christos 	}
   2035  1.1  christos #endif
   2036  1.1  christos 	return (isc_entropy_status(dst_entropy_pool));
   2037  1.1  christos #else
   2038  1.1  christos 	/* Doesn't matter as it is not used in this case. */
   2039  1.1  christos 	return (0);
   2040  1.1  christos #endif
   2041  1.1  christos }
   2042  1.1  christos 
   2043  1.1  christos isc_buffer_t *
   2044  1.1  christos dst_key_tkeytoken(const dst_key_t *key) {
   2045  1.1  christos 	REQUIRE(VALID_KEY(key));
   2046  1.1  christos 	return (key->key_tkeytoken);
   2047  1.1  christos }
   2048