dst_api.c revision e672951ed28b2e9cc7a19c3d7fa4a258382f981c
609b8d08176469485edce25f3c2f50365bbd3819Mark Andrews/*
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Portions Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Portions Copyright (C) 1999-2003 Internet Software Consortium.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and/or distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
f97d56e757b9a293ffbaa915ca4d792ae84ba85aTinderbox User * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f97d56e757b9a293ffbaa915ca4d792ae84ba85aTinderbox User *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * purpose with or without fee is hereby granted, provided that the above
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * copyright notice and this permission notice appear in all copies.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver */
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt/*
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Principal Author: Brian Wellington
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * $Id: dst_api.c,v 1.14 2008/04/01 23:47:10 tbox Exp $
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews/*! \file */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <config.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <stdlib.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/buffer.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/dir.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/entropy.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/fsaccess.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/hmacsha.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <isc/lex.h>
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#include <isc/mem.h>
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#include <isc/once.h>
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#include <isc/print.h>
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#include <isc/random.h>
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#include <isc/string.h>
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#include <isc/time.h>
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#include <isc/util.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver#include <dns/fixedname.h>
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver#include <dns/keyvalues.h>
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#include <dns/name.h>
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#include <dns/rdata.h>
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#include <dns/rdataclass.h>
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#include <dns/ttl.h>
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#include <dns/types.h>
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#include <dst/result.h>
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#include "dst_internal.h"
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#define DST_AS_STR(t) ((t).value.as_textregion.base)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverstatic dst_func_t *dst_t_func[DST_MAX_ALGS];
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic isc_entropy_t *dst_entropy_pool = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic unsigned int dst_entropy_flags = 0;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsstatic isc_boolean_t dst_initialized = ISC_FALSE;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryvervoid gss_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_mem_t *dst__memory_pool = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
88112d5fcb6b9e1d0f80105a9d2a6f09440c2401Mark Andrews/*
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * Static functions.
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic dst_key_t * get_key_struct(dns_name_t *name,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews unsigned int alg,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews unsigned int flags,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews unsigned int protocol,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews unsigned int bits,
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt dns_rdataclass_t rdclass,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_mem_t *mctx);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsstatic isc_result_t write_public_key(const dst_key_t *key, int type,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews const char *directory);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic isc_result_t buildfilename(dns_name_t *name,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dns_keytag_t id,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver unsigned int alg,
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt unsigned int type,
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt const char *directory,
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt isc_buffer_t *out);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic isc_result_t computeid(dst_key_t *key);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Huntstatic isc_result_t frombuffer(dns_name_t *name,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver unsigned int alg,
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt unsigned int flags,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver unsigned int protocol,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dns_rdataclass_t rdclass,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_buffer_t *source,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_mem_t *mctx,
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver dst_key_t **keyp);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverstatic isc_result_t algorithm_status(unsigned int alg);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverstatic isc_result_t addsuffix(char *filename, unsigned int len,
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver const char *ofilename, const char *suffix);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver#define RETERR(x) \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver do { \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver result = (x); \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (result != ISC_R_SUCCESS) \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver goto out; \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver } while (0)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver#define CHECKALG(alg) \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver do { \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_result_t _r; \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver _r = algorithm_status(alg); \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (_r != ISC_R_SUCCESS) \
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (_r); \
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver } while (0); \
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic void *
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverdefault_memalloc(void *arg, size_t size) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver UNUSED(arg);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (size == 0U)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver size = 1;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (malloc(size));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver}
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverstatic void
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverdefault_memfree(void *arg, void *ptr) {
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver UNUSED(arg);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver free(ptr);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_result_t
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverdst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags) {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_result_t result;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt REQUIRE(mctx != NULL && ectx != NULL);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt REQUIRE(dst_initialized == ISC_FALSE);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt dst__memory_pool = NULL;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt#ifdef OPENSSL
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt UNUSED(mctx);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt /*
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * When using --with-openssl, there seems to be no good way of not
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * leaking memory due to the openssl error handling mechanism.
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * Avoid assertions by using a local memory context and not checking
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt * for leaks on exit. Note: as there are leaks we cannot use
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * ISC_MEMFLAG_INTERNAL as it will free up memory still being used
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt * by libcrypto.
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver NULL, &dst__memory_pool, 0);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (result != ISC_R_SUCCESS)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (result);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_mem_setname(dst__memory_pool, "dst", NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_mem_setdestroycheck(dst__memory_pool, ISC_FALSE);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#else
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_mem_attach(mctx, &dst__memory_pool);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#endif
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_entropy_attach(ectx, &dst_entropy_pool);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_entropy_flags = eflags;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_result_register();
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver memset(dst_t_func, 0, sizeof(dst_t_func));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt RETERR(dst__hmacmd5_init(&dst_t_func[DST_ALG_HMACMD5]));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RETERR(dst__hmacsha1_init(&dst_t_func[DST_ALG_HMACSHA1]));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RETERR(dst__hmacsha224_init(&dst_t_func[DST_ALG_HMACSHA224]));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RETERR(dst__hmacsha256_init(&dst_t_func[DST_ALG_HMACSHA256]));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver RETERR(dst__hmacsha384_init(&dst_t_func[DST_ALG_HMACSHA384]));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RETERR(dst__hmacsha512_init(&dst_t_func[DST_ALG_HMACSHA512]));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#ifdef OPENSSL
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RETERR(dst__openssl_init());
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSAMD5]));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA1]));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#ifdef HAVE_OPENSSL_DSA
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt RETERR(dst__openssldsa_init(&dst_t_func[DST_ALG_DSA]));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt#endif
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt RETERR(dst__openssldh_init(&dst_t_func[DST_ALG_DH]));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#endif /* OPENSSL */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#ifdef GSSAPI
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver RETERR(dst__gssapi_init(&dst_t_func[DST_ALG_GSSAPI]));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver#endif
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt dst_initialized = ISC_TRUE;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (ISC_R_SUCCESS);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt out:
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt dst_lib_destroy();
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (result);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryvervoid
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverdst_lib_destroy(void) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver int i;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RUNTIME_CHECK(dst_initialized == ISC_TRUE);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_initialized = ISC_FALSE;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver for (i = 0; i < DST_MAX_ALGS; i++)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (dst_t_func[i] != NULL && dst_t_func[i]->cleanup != NULL)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver dst_t_func[i]->cleanup();
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver#ifdef OPENSSL
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst__openssl_destroy();
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#endif
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (dst__memory_pool != NULL)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_mem_detach(&dst__memory_pool);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (dst_entropy_pool != NULL)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_entropy_detach(&dst_entropy_pool);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver}
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverisc_boolean_t
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverdst_algorithm_supported(unsigned int alg) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(dst_initialized == ISC_TRUE);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (ISC_FALSE);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (ISC_TRUE);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverisc_result_t
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverdst_context_create(dst_key_t *key, isc_mem_t *mctx, dst_context_t **dctxp) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_context_t *dctx;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_result_t result;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(dst_initialized == ISC_TRUE);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(VALID_KEY(key));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(mctx != NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(dctxp != NULL && *dctxp == NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (key->func->createctx == NULL)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (DST_R_UNSUPPORTEDALG);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (key->keydata.generic == NULL)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (DST_R_NULLKEY);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver dctx = isc_mem_get(mctx, sizeof(dst_context_t));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (dctx == NULL)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (ISC_R_NOMEMORY);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dctx->key = key;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver dctx->mctx = mctx;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver result = key->func->createctx(key, dctx);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (result != ISC_R_SUCCESS) {
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_mem_put(mctx, dctx, sizeof(dst_context_t));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (result);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews }
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews dctx->magic = CTX_MAGIC;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt *dctxp = dctx;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (ISC_R_SUCCESS);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt}
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsvoid
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverdst_context_destroy(dst_context_t **dctxp) {
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver dst_context_t *dctx;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(dctxp != NULL && VALID_CTX(*dctxp));
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews dctx = *dctxp;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver INSIST(dctx->key->func->destroyctx != NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dctx->key->func->destroyctx(dctx);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dctx->magic = 0;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_mem_put(dctx->mctx, dctx, sizeof(dst_context_t));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver *dctxp = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_result_t
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Huntdst_context_adddata(dst_context_t *dctx, const isc_region_t *data) {
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt REQUIRE(VALID_CTX(dctx));
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt REQUIRE(data != NULL);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt INSIST(dctx->key->func->adddata != NULL);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (dctx->key->func->adddata(dctx, data));
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt}
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Huntisc_result_t
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Huntdst_context_sign(dst_context_t *dctx, isc_buffer_t *sig) {
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt dst_key_t *key;
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt REQUIRE(VALID_CTX(dctx));
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt REQUIRE(sig != NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver key = dctx->key;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver CHECKALG(key->key_alg);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (key->keydata.generic == NULL)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (DST_R_NULLKEY);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (key->func->sign == NULL)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (DST_R_NOTPRIVATEKEY);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (key->func->isprivate == NULL ||
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver key->func->isprivate(key) == ISC_FALSE)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (DST_R_NOTPRIVATEKEY);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (key->func->sign(dctx, sig));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_result_t
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverdst_context_verify(dst_context_t *dctx, isc_region_t *sig) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(VALID_CTX(dctx));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(sig != NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver CHECKALG(dctx->key->key_alg);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (dctx->key->keydata.generic == NULL)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (DST_R_NULLKEY);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (dctx->key->func->verify == NULL)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (DST_R_NOTPUBLICKEY);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (dctx->key->func->verify(dctx, sig));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt}
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntisc_result_t
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntdst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_buffer_t *secret)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver{
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(dst_initialized == ISC_TRUE);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(VALID_KEY(pub) && VALID_KEY(priv));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(secret != NULL);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt CHECKALG(pub->key_alg);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt CHECKALG(priv->key_alg);
9fa2a0deed3b880f3bf04d4f615c13a0d67cc0ceMark Andrews
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (pub->keydata.generic == NULL || priv->keydata.generic == NULL)
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (DST_R_NULLKEY);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (pub->key_alg != priv->key_alg ||
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt pub->func->computesecret == NULL ||
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver priv->func->computesecret == NULL)
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (DST_R_KEYCANNOTCOMPUTESECRET);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (dst_key_isprivate(priv) == ISC_FALSE)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (DST_R_NOTPRIVATEKEY);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (pub->func->computesecret(pub, priv, secret));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver}
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverisc_result_t
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverdst_key_tofile(const dst_key_t *key, int type, const char *directory) {
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_result_t ret = ISC_R_SUCCESS;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(dst_initialized == ISC_TRUE);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(VALID_KEY(key));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt CHECKALG(key->key_alg);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (key->func->tofile == NULL)
75c622f53bdda9d2f69f05e06eaf7be01fc09a33Evan Hunt return (DST_R_UNSUPPORTEDALG);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews if (type & DST_TYPE_PUBLIC) {
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews ret = write_public_key(key, type, directory);
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews if (ret != ISC_R_SUCCESS)
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews return (ret);
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews }
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews if ((type & DST_TYPE_PRIVATE) &&
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews (key->key_flags & DNS_KEYFLAG_TYPEMASK) != DNS_KEYTYPE_NOKEY)
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews return (key->func->tofile(key, directory));
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews else
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews return (ISC_R_SUCCESS);
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews}
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrewsisc_result_t
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrewsdst_key_fromfile(dns_name_t *name, dns_keytag_t id,
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews unsigned int alg, int type, const char *directory,
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews isc_mem_t *mctx, dst_key_t **keyp)
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews{
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews char filename[ISC_DIR_NAMEMAX];
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews isc_buffer_t b;
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews dst_key_t *key;
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews isc_result_t result;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews REQUIRE(dst_initialized == ISC_TRUE);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews REQUIRE(dns_name_isabsolute(name));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(mctx != NULL);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews REQUIRE(keyp != NULL && *keyp == NULL);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews CHECKALG(alg);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_buffer_init(&b, filename, sizeof(filename));
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews result = buildfilename(name, id, alg, type, directory, &b);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (result != ISC_R_SUCCESS)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (result);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews key = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver result = dst_key_fromnamedfile(filename, type, mctx, &key);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (result != ISC_R_SUCCESS)
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (result);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews result = computeid(key);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (result != ISC_R_SUCCESS) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_key_free(&key);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (result);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver }
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (!dns_name_equal(name, key->key_name) || id != key->key_id ||
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver alg != key->key_alg) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_key_free(&key);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (DST_R_INVALIDPRIVATEKEY);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver }
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver key->key_id = id;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver *keyp = key;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (ISC_R_SUCCESS);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_result_t
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntdst_key_fromnamedfile(const char *filename, int type, isc_mem_t *mctx,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews dst_key_t **keyp)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews{
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_result_t result;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt dst_key_t *pubkey = NULL, *key = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dns_keytag_t id;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt char *newfilename = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver int newfilenamelen = 0;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_lex_t *lex = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(dst_initialized == ISC_TRUE);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(filename != NULL);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(mctx != NULL);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(keyp != NULL && *keyp == NULL);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt newfilenamelen = strlen(filename) + 5;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt newfilename = isc_mem_get(mctx, newfilenamelen);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (newfilename == NULL)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (ISC_R_NOMEMORY);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt result = addsuffix(newfilename, newfilenamelen, filename, ".key");
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews INSIST(result == ISC_R_SUCCESS);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews result = dst_key_read_public(newfilename, type, mctx, &pubkey);
76db58eb818dc4839fa816df6a1a1ecb2c7a6bd0Evan Hunt isc_mem_put(mctx, newfilename, newfilenamelen);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver newfilename = NULL;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (result != ISC_R_SUCCESS)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (result);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if ((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) == DST_TYPE_PUBLIC ||
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver (pubkey->key_flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver result = computeid(pubkey);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (result != ISC_R_SUCCESS) {
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt dst_key_free(&pubkey);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (result);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt }
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt *keyp = pubkey;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (ISC_R_SUCCESS);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver }
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver result = algorithm_status(pubkey->key_alg);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (result != ISC_R_SUCCESS) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_key_free(&pubkey);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (result);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver }
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver key = get_key_struct(pubkey->key_name, pubkey->key_alg,
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver pubkey->key_flags, pubkey->key_proto, 0,
9fa2a0deed3b880f3bf04d4f615c13a0d67cc0ceMark Andrews pubkey->key_class, mctx);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt id = pubkey->key_id;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver dst_key_free(&pubkey);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (key == NULL)
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt return (ISC_R_NOMEMORY);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (key->func->parse == NULL)
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt RETERR(DST_R_UNSUPPORTEDALG);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver newfilenamelen = strlen(filename) + 9;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver newfilename = isc_mem_get(mctx, newfilenamelen);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (newfilename == NULL)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RETERR(ISC_R_NOMEMORY);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver result = addsuffix(newfilename, newfilenamelen, filename, ".private");
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver INSIST(result == ISC_R_SUCCESS);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt RETERR(isc_lex_create(mctx, 1500, &lex));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt RETERR(isc_lex_openfile(lex, newfilename));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_mem_put(mctx, newfilename, newfilenamelen);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RETERR(key->func->parse(key, lex));
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt isc_lex_destroy(&lex);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt
475b1ed9cced1f92ce34bc2e59b3065dae48f366Mark Andrews RETERR(computeid(key));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (id != key->key_id)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver RETERR(DST_R_INVALIDPRIVATEKEY);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver *keyp = key;
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (ISC_R_SUCCESS);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt out:
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (newfilename != NULL)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_mem_put(mctx, newfilename, newfilenamelen);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (lex != NULL)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_lex_destroy(&lex);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt dst_key_free(&key);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (result);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt}
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntisc_result_t
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntdst_key_todns(const dst_key_t *key, isc_buffer_t *target) {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(dst_initialized == ISC_TRUE);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(VALID_KEY(key));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(target != NULL);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver CHECKALG(key->key_alg);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (key->func->todns == NULL)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (DST_R_UNSUPPORTEDALG);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (isc_buffer_availablelength(target) < 4)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (ISC_R_NOSPACE);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_buffer_putuint16(target, (isc_uint16_t)(key->key_flags & 0xffff));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_buffer_putuint8(target, (isc_uint8_t)key->key_proto);
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt isc_buffer_putuint8(target, (isc_uint8_t)key->key_alg);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt if (key->key_flags & DNS_KEYFLAG_EXTENDED) {
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver if (isc_buffer_availablelength(target) < 2)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver return (ISC_R_NOSPACE);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_buffer_putuint16(target,
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Hunt (isc_uint16_t)((key->key_flags >> 16)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt & 0xffff));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt }
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt if (key->keydata.generic == NULL) /*%< NULL KEY */
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (ISC_R_SUCCESS);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (key->func->todns(key, target));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt}
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Huntisc_result_t
421d4a06479e61fbdc35087f3c4abc9fe65ad72aEvan Huntdst_key_fromdns(dns_name_t *name, dns_rdataclass_t rdclass,
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver{
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_uint8_t alg, proto;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_uint32_t flags, extflags;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver dst_key_t *key = NULL;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver dns_keytag_t id;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_region_t r;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_result_t result;
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(dst_initialized);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver isc_buffer_remainingregion(source, &r);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (isc_buffer_remaininglength(source) < 4)
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt return (DST_R_INVALIDPUBLICKEY);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt flags = isc_buffer_getuint16(source);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt proto = isc_buffer_getuint8(source);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt alg = isc_buffer_getuint8(source);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt id = dst_region_computeid(&r, alg);
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
225146b2c8c7de8dcff979841b56b15aef8aded2Mark Andrews if (flags & DNS_KEYFLAG_EXTENDED) {
225146b2c8c7de8dcff979841b56b15aef8aded2Mark Andrews if (isc_buffer_remaininglength(source) < 2)
225146b2c8c7de8dcff979841b56b15aef8aded2Mark Andrews return (DST_R_INVALIDPUBLICKEY);
225146b2c8c7de8dcff979841b56b15aef8aded2Mark Andrews extflags = isc_buffer_getuint16(source);
225146b2c8c7de8dcff979841b56b15aef8aded2Mark Andrews flags |= (extflags << 16);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews }
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
result = frombuffer(name, alg, flags, proto, rdclass, source,
mctx, &key);
if (result != ISC_R_SUCCESS)
return (result);
key->key_id = id;
*keyp = key;
return (ISC_R_SUCCESS);
}
isc_result_t
dst_key_frombuffer(dns_name_t *name, unsigned int alg,
unsigned int flags, unsigned int protocol,
dns_rdataclass_t rdclass,
isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
{
dst_key_t *key = NULL;
isc_result_t result;
REQUIRE(dst_initialized);
result = frombuffer(name, alg, flags, protocol, rdclass, source,
mctx, &key);
if (result != ISC_R_SUCCESS)
return (result);
result = computeid(key);
if (result != ISC_R_SUCCESS) {
dst_key_free(&key);
return (result);
}
*keyp = key;
return (ISC_R_SUCCESS);
}
isc_result_t
dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target) {
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(VALID_KEY(key));
REQUIRE(target != NULL);
CHECKALG(key->key_alg);
if (key->func->todns == NULL)
return (DST_R_UNSUPPORTEDALG);
return (key->func->todns(key, target));
}
isc_result_t
dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer) {
isc_lex_t *lex = NULL;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(VALID_KEY(key));
REQUIRE(!dst_key_isprivate(key));
REQUIRE(buffer != NULL);
if (key->func->parse == NULL)
RETERR(DST_R_UNSUPPORTEDALG);
RETERR(isc_lex_create(key->mctx, 1500, &lex));
RETERR(isc_lex_openbuffer(lex, buffer));
RETERR(key->func->parse(key, lex));
out:
if (lex != NULL)
isc_lex_destroy(&lex);
return (result);
}
gss_ctx_id_t
dst_key_getgssctx(const dst_key_t *key)
{
REQUIRE(key != NULL);
return (key->keydata.gssctx);
}
isc_result_t
dst_key_fromgssapi(dns_name_t *name, gss_ctx_id_t gssctx, isc_mem_t *mctx,
dst_key_t **keyp)
{
dst_key_t *key;
REQUIRE(gssctx != NULL);
REQUIRE(keyp != NULL && *keyp == NULL);
key = get_key_struct(name, DST_ALG_GSSAPI, 0, DNS_KEYPROTO_DNSSEC,
0, dns_rdataclass_in, mctx);
if (key == NULL)
return (ISC_R_NOMEMORY);
key->keydata.gssctx = gssctx;
*keyp = key;
return (ISC_R_SUCCESS);
}
isc_result_t
dst_key_fromlabel(dns_name_t *name, int alg, unsigned int flags,
unsigned int protocol, dns_rdataclass_t rdclass,
const char *engine, const char *label, const char *pin,
isc_mem_t *mctx, dst_key_t **keyp)
{
dst_key_t *key;
isc_result_t result;
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(dns_name_isabsolute(name));
REQUIRE(mctx != NULL);
REQUIRE(keyp != NULL && *keyp == NULL);
REQUIRE(label != NULL);
CHECKALG(alg);
key = get_key_struct(name, alg, flags, protocol, 0, rdclass, mctx);
if (key == NULL)
return (ISC_R_NOMEMORY);
if (key->func->fromlabel == NULL) {
dst_key_free(&key);
return (DST_R_UNSUPPORTEDALG);
}
result = key->func->fromlabel(key, engine, label, pin);
if (result != ISC_R_SUCCESS) {
dst_key_free(&key);
return (result);
}
result = computeid(key);
if (result != ISC_R_SUCCESS) {
dst_key_free(&key);
return (result);
}
*keyp = key;
return (ISC_R_SUCCESS);
}
isc_result_t
dst_key_generate(dns_name_t *name, unsigned int alg,
unsigned int bits, unsigned int param,
unsigned int flags, unsigned int protocol,
dns_rdataclass_t rdclass,
isc_mem_t *mctx, dst_key_t **keyp)
{
dst_key_t *key;
isc_result_t ret;
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(dns_name_isabsolute(name));
REQUIRE(mctx != NULL);
REQUIRE(keyp != NULL && *keyp == NULL);
CHECKALG(alg);
key = get_key_struct(name, alg, flags, protocol, bits, rdclass, mctx);
if (key == NULL)
return (ISC_R_NOMEMORY);
if (bits == 0) { /*%< NULL KEY */
key->key_flags |= DNS_KEYTYPE_NOKEY;
*keyp = key;
return (ISC_R_SUCCESS);
}
if (key->func->generate == NULL) {
dst_key_free(&key);
return (DST_R_UNSUPPORTEDALG);
}
ret = key->func->generate(key, param);
if (ret != ISC_R_SUCCESS) {
dst_key_free(&key);
return (ret);
}
ret = computeid(key);
if (ret != ISC_R_SUCCESS) {
dst_key_free(&key);
return (ret);
}
*keyp = key;
return (ISC_R_SUCCESS);
}
isc_boolean_t
dst_key_compare(const dst_key_t *key1, const dst_key_t *key2) {
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(VALID_KEY(key1));
REQUIRE(VALID_KEY(key2));
if (key1 == key2)
return (ISC_TRUE);
if (key1 == NULL || key2 == NULL)
return (ISC_FALSE);
if (key1->key_alg == key2->key_alg &&
key1->key_id == key2->key_id &&
key1->func->compare != NULL &&
key1->func->compare(key1, key2) == ISC_TRUE)
return (ISC_TRUE);
else
return (ISC_FALSE);
}
isc_boolean_t
dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2) {
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(VALID_KEY(key1));
REQUIRE(VALID_KEY(key2));
if (key1 == key2)
return (ISC_TRUE);
if (key1 == NULL || key2 == NULL)
return (ISC_FALSE);
if (key1->key_alg == key2->key_alg &&
key1->func->paramcompare != NULL &&
key1->func->paramcompare(key1, key2) == ISC_TRUE)
return (ISC_TRUE);
else
return (ISC_FALSE);
}
void
dst_key_free(dst_key_t **keyp) {
isc_mem_t *mctx;
dst_key_t *key;
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(keyp != NULL && VALID_KEY(*keyp));
key = *keyp;
mctx = key->mctx;
if (key->keydata.generic != NULL) {
INSIST(key->func->destroy != NULL);
key->func->destroy(key);
}
if (key->engine != NULL)
isc_mem_free(mctx, key->engine);
if (key->label != NULL)
isc_mem_free(mctx, key->label);
dns_name_free(key->key_name, mctx);
isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
memset(key, 0, sizeof(dst_key_t));
isc_mem_put(mctx, key, sizeof(dst_key_t));
*keyp = NULL;
}
isc_boolean_t
dst_key_isprivate(const dst_key_t *key) {
REQUIRE(VALID_KEY(key));
INSIST(key->func->isprivate != NULL);
return (key->func->isprivate(key));
}
isc_result_t
dst_key_buildfilename(const dst_key_t *key, int type,
const char *directory, isc_buffer_t *out) {
REQUIRE(VALID_KEY(key));
REQUIRE(type == DST_TYPE_PRIVATE || type == DST_TYPE_PUBLIC ||
type == 0);
return (buildfilename(key->key_name, key->key_id, key->key_alg,
type, directory, out));
}
isc_result_t
dst_key_sigsize(const dst_key_t *key, unsigned int *n) {
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(VALID_KEY(key));
REQUIRE(n != NULL);
/* XXXVIX this switch statement is too sparse to gen a jump table. */
switch (key->key_alg) {
case DST_ALG_RSAMD5:
case DST_ALG_RSASHA1:
*n = (key->key_size + 7) / 8;
break;
case DST_ALG_DSA:
*n = DNS_SIG_DSASIGSIZE;
break;
case DST_ALG_HMACMD5:
*n = 16;
break;
case DST_ALG_HMACSHA1:
*n = ISC_SHA1_DIGESTLENGTH;
break;
case DST_ALG_HMACSHA224:
*n = ISC_SHA224_DIGESTLENGTH;
break;
case DST_ALG_HMACSHA256:
*n = ISC_SHA256_DIGESTLENGTH;
break;
case DST_ALG_HMACSHA384:
*n = ISC_SHA384_DIGESTLENGTH;
break;
case DST_ALG_HMACSHA512:
*n = ISC_SHA512_DIGESTLENGTH;
break;
case DST_ALG_GSSAPI:
*n = 128; /*%< XXX */
break;
case DST_ALG_DH:
default:
return (DST_R_UNSUPPORTEDALG);
}
return (ISC_R_SUCCESS);
}
isc_result_t
dst_key_secretsize(const dst_key_t *key, unsigned int *n) {
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(VALID_KEY(key));
REQUIRE(n != NULL);
if (key->key_alg == DST_ALG_DH)
*n = (key->key_size + 7) / 8;
else
return (DST_R_UNSUPPORTEDALG);
return (ISC_R_SUCCESS);
}
/***
*** Static methods
***/
/*%
* Allocates a key structure and fills in some of the fields.
*/
static dst_key_t *
get_key_struct(dns_name_t *name, unsigned int alg,
unsigned int flags, unsigned int protocol,
unsigned int bits, dns_rdataclass_t rdclass,
isc_mem_t *mctx)
{
dst_key_t *key;
isc_result_t result;
key = (dst_key_t *) isc_mem_get(mctx, sizeof(dst_key_t));
if (key == NULL)
return (NULL);
memset(key, 0, sizeof(dst_key_t));
key->magic = KEY_MAGIC;
key->key_name = isc_mem_get(mctx, sizeof(dns_name_t));
if (key->key_name == NULL) {
isc_mem_put(mctx, key, sizeof(dst_key_t));
return (NULL);
}
dns_name_init(key->key_name, NULL);
result = dns_name_dup(name, mctx, key->key_name);
if (result != ISC_R_SUCCESS) {
isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
isc_mem_put(mctx, key, sizeof(dst_key_t));
return (NULL);
}
key->key_alg = alg;
key->key_flags = flags;
key->key_proto = protocol;
key->mctx = mctx;
key->keydata.generic = NULL;
key->key_size = bits;
key->key_class = rdclass;
key->func = dst_t_func[alg];
return (key);
}
/*%
* Reads a public key from disk
*/
isc_result_t
dst_key_read_public(const char *filename, int type,
isc_mem_t *mctx, dst_key_t **keyp)
{
u_char rdatabuf[DST_KEY_MAXSIZE];
isc_buffer_t b;
dns_fixedname_t name;
isc_lex_t *lex = NULL;
isc_token_t token;
isc_result_t ret;
dns_rdata_t rdata = DNS_RDATA_INIT;
unsigned int opt = ISC_LEXOPT_DNSMULTILINE;
dns_rdataclass_t rdclass = dns_rdataclass_in;
isc_lexspecials_t specials;
isc_uint32_t ttl;
isc_result_t result;
dns_rdatatype_t keytype;
/*
* Open the file and read its formatted contents
* File format:
* domain.name [ttl] [class] [KEY|DNSKEY] <flags> <protocol> <algorithm> <key>
*/
/* 1500 should be large enough for any key */
ret = isc_lex_create(mctx, 1500, &lex);
if (ret != ISC_R_SUCCESS)
goto cleanup;
memset(specials, 0, sizeof(specials));
specials['('] = 1;
specials[')'] = 1;
specials['"'] = 1;
isc_lex_setspecials(lex, specials);
isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
ret = isc_lex_openfile(lex, filename);
if (ret != ISC_R_SUCCESS)
goto cleanup;
#define NEXTTOKEN(lex, opt, token) { \
ret = isc_lex_gettoken(lex, opt, token); \
if (ret != ISC_R_SUCCESS) \
goto cleanup; \
}
#define BADTOKEN() { \
ret = ISC_R_UNEXPECTEDTOKEN; \
goto cleanup; \
}
/* Read the domain name */
NEXTTOKEN(lex, opt, &token);
if (token.type != isc_tokentype_string)
BADTOKEN();
dns_fixedname_init(&name);
isc_buffer_init(&b, DST_AS_STR(token), strlen(DST_AS_STR(token)));
isc_buffer_add(&b, strlen(DST_AS_STR(token)));
ret = dns_name_fromtext(dns_fixedname_name(&name), &b, dns_rootname,
ISC_FALSE, NULL);
if (ret != ISC_R_SUCCESS)
goto cleanup;
/* Read the next word: either TTL, class, or 'KEY' */
NEXTTOKEN(lex, opt, &token);
/* If it's a TTL, read the next one */
result = dns_ttl_fromtext(&token.value.as_textregion, &ttl);
if (result == ISC_R_SUCCESS)
NEXTTOKEN(lex, opt, &token);
if (token.type != isc_tokentype_string)
BADTOKEN();
ret = dns_rdataclass_fromtext(&rdclass, &token.value.as_textregion);
if (ret == ISC_R_SUCCESS)
NEXTTOKEN(lex, opt, &token);
if (token.type != isc_tokentype_string)
BADTOKEN();
if (strcasecmp(DST_AS_STR(token), "DNSKEY") == 0)
keytype = dns_rdatatype_dnskey;
else if (strcasecmp(DST_AS_STR(token), "KEY") == 0)
keytype = dns_rdatatype_key; /*%< SIG(0), TKEY */
else
BADTOKEN();
if (((type & DST_TYPE_KEY) != 0 && keytype != dns_rdatatype_key) ||
((type & DST_TYPE_KEY) == 0 && keytype != dns_rdatatype_dnskey)) {
ret = DST_R_BADKEYTYPE;
goto cleanup;
}
isc_buffer_init(&b, rdatabuf, sizeof(rdatabuf));
ret = dns_rdata_fromtext(&rdata, rdclass, keytype, lex, NULL,
ISC_FALSE, mctx, &b, NULL);
if (ret != ISC_R_SUCCESS)
goto cleanup;
ret = dst_key_fromdns(dns_fixedname_name(&name), rdclass, &b, mctx,
keyp);
if (ret != ISC_R_SUCCESS)
goto cleanup;
cleanup:
if (lex != NULL)
isc_lex_destroy(&lex);
return (ret);
}
static isc_boolean_t
issymmetric(const dst_key_t *key) {
REQUIRE(dst_initialized == ISC_TRUE);
REQUIRE(VALID_KEY(key));
/* XXXVIX this switch statement is too sparse to gen a jump table. */
switch (key->key_alg) {
case DST_ALG_RSAMD5:
case DST_ALG_RSASHA1:
case DST_ALG_DSA:
case DST_ALG_DH:
return (ISC_FALSE);
case DST_ALG_HMACMD5:
case DST_ALG_GSSAPI:
return (ISC_TRUE);
default:
return (ISC_FALSE);
}
}
/*%
* Writes a public key to disk in DNS format.
*/
static isc_result_t
write_public_key(const dst_key_t *key, int type, const char *directory) {
FILE *fp;
isc_buffer_t keyb, textb, fileb, classb;
isc_region_t r;
char filename[ISC_DIR_NAMEMAX];
unsigned char key_array[DST_KEY_MAXSIZE];
char text_array[DST_KEY_MAXTEXTSIZE];
char class_array[10];
isc_result_t ret;
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_fsaccess_t access;
REQUIRE(VALID_KEY(key));
isc_buffer_init(&keyb, key_array, sizeof(key_array));
isc_buffer_init(&textb, text_array, sizeof(text_array));
isc_buffer_init(&classb, class_array, sizeof(class_array));
ret = dst_key_todns(key, &keyb);
if (ret != ISC_R_SUCCESS)
return (ret);
isc_buffer_usedregion(&keyb, &r);
dns_rdata_fromregion(&rdata, key->key_class, dns_rdatatype_dnskey, &r);
ret = dns_rdata_totext(&rdata, (dns_name_t *) NULL, &textb);
if (ret != ISC_R_SUCCESS)
return (DST_R_INVALIDPUBLICKEY);
ret = dns_rdataclass_totext(key->key_class, &classb);
if (ret != ISC_R_SUCCESS)
return (DST_R_INVALIDPUBLICKEY);
/*
* Make the filename.
*/
isc_buffer_init(&fileb, filename, sizeof(filename));
ret = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory, &fileb);
if (ret != ISC_R_SUCCESS)
return (ret);
/*
* Create public key file.
*/
if ((fp = fopen(filename, "w")) == NULL)
return (DST_R_WRITEERROR);
if (issymmetric(key)) {
access = 0;
isc_fsaccess_add(ISC_FSACCESS_OWNER,
ISC_FSACCESS_READ | ISC_FSACCESS_WRITE,
&access);
(void)isc_fsaccess_set(filename, access);
}
ret = dns_name_print(key->key_name, fp);
if (ret != ISC_R_SUCCESS) {
fclose(fp);
return (ret);
}
fprintf(fp, " ");
isc_buffer_usedregion(&classb, &r);
fwrite(r.base, 1, r.length, fp);
if ((type & DST_TYPE_KEY) != 0)
fprintf(fp, " KEY ");
else
fprintf(fp, " DNSKEY ");
isc_buffer_usedregion(&textb, &r);
fwrite(r.base, 1, r.length, fp);
fputc('\n', fp);
fclose(fp);
return (ISC_R_SUCCESS);
}
static isc_result_t
buildfilename(dns_name_t *name, dns_keytag_t id,
unsigned int alg, unsigned int type,
const char *directory, isc_buffer_t *out)
{
const char *suffix = "";
unsigned int len;
isc_result_t result;
REQUIRE(out != NULL);
if ((type & DST_TYPE_PRIVATE) != 0)
suffix = ".private";
else if (type == DST_TYPE_PUBLIC)
suffix = ".key";
if (directory != NULL) {
if (isc_buffer_availablelength(out) < strlen(directory))
return (ISC_R_NOSPACE);
isc_buffer_putstr(out, directory);
if (strlen(directory) > 0U &&
directory[strlen(directory) - 1] != '/')
isc_buffer_putstr(out, "/");
}
if (isc_buffer_availablelength(out) < 1)
return (ISC_R_NOSPACE);
isc_buffer_putstr(out, "K");
result = dns_name_tofilenametext(name, ISC_FALSE, out);
if (result != ISC_R_SUCCESS)
return (result);
len = 1 + 3 + 1 + 5 + strlen(suffix) + 1;
if (isc_buffer_availablelength(out) < len)
return (ISC_R_NOSPACE);
sprintf((char *) isc_buffer_used(out), "+%03d+%05d%s", alg, id,
suffix);
isc_buffer_add(out, len);
return (ISC_R_SUCCESS);
}
static isc_result_t
computeid(dst_key_t *key) {
isc_buffer_t dnsbuf;
unsigned char dns_array[DST_KEY_MAXSIZE];
isc_region_t r;
isc_result_t ret;
isc_buffer_init(&dnsbuf, dns_array, sizeof(dns_array));
ret = dst_key_todns(key, &dnsbuf);
if (ret != ISC_R_SUCCESS)
return (ret);
isc_buffer_usedregion(&dnsbuf, &r);
key->key_id = dst_region_computeid(&r, key->key_alg);
return (ISC_R_SUCCESS);
}
static isc_result_t
frombuffer(dns_name_t *name, unsigned int alg, unsigned int flags,
unsigned int protocol, dns_rdataclass_t rdclass,
isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
{
dst_key_t *key;
isc_result_t ret;
REQUIRE(dns_name_isabsolute(name));
REQUIRE(source != NULL);
REQUIRE(mctx != NULL);
REQUIRE(keyp != NULL && *keyp == NULL);
key = get_key_struct(name, alg, flags, protocol, 0, rdclass, mctx);
if (key == NULL)
return (ISC_R_NOMEMORY);
if (isc_buffer_remaininglength(source) > 0) {
ret = algorithm_status(alg);
if (ret != ISC_R_SUCCESS) {
dst_key_free(&key);
return (ret);
}
if (key->func->fromdns == NULL) {
dst_key_free(&key);
return (DST_R_UNSUPPORTEDALG);
}
ret = key->func->fromdns(key, source);
if (ret != ISC_R_SUCCESS) {
dst_key_free(&key);
return (ret);
}
}
*keyp = key;
return (ISC_R_SUCCESS);
}
static isc_result_t
algorithm_status(unsigned int alg) {
REQUIRE(dst_initialized == ISC_TRUE);
if (dst_algorithm_supported(alg))
return (ISC_R_SUCCESS);
#ifndef OPENSSL
if (alg == DST_ALG_RSAMD5 || alg == DST_ALG_RSASHA1 ||
alg == DST_ALG_DSA || alg == DST_ALG_DH ||
alg == DST_ALG_HMACMD5)
return (DST_R_NOCRYPTO);
#endif
return (DST_R_UNSUPPORTEDALG);
}
static isc_result_t
addsuffix(char *filename, unsigned int len, const char *ofilename,
const char *suffix)
{
int olen = strlen(ofilename);
int n;
if (olen > 1 && ofilename[olen - 1] == '.')
olen -= 1;
else if (olen > 8 && strcmp(ofilename + olen - 8, ".private") == 0)
olen -= 8;
else if (olen > 4 && strcmp(ofilename + olen - 4, ".key") == 0)
olen -= 4;
n = snprintf(filename, len, "%.*s%s", olen, ofilename, suffix);
if (n < 0)
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
}
isc_result_t
dst__entropy_getdata(void *buf, unsigned int len, isc_boolean_t pseudo) {
unsigned int flags = dst_entropy_flags;
if (pseudo)
flags &= ~ISC_ENTROPY_GOODONLY;
return (isc_entropy_getdata(dst_entropy_pool, buf, len, NULL, flags));
}
unsigned int
dst__entropy_status(void) {
return (isc_entropy_status(dst_entropy_pool));
}