2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/lib/dns/openssl_link.c

323 lines
7.1 KiB
C
Raw Normal View History

/*
2012-03-10 23:45:53 +00:00
* Portions Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
2004-03-05 05:14:21 +00:00
* Portions Copyright (C) 1999-2003 Internet Software Consortium.
2007-06-18 23:47:57 +00:00
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Portions Copyright (C) 1995-2000 by Network Associates, Inc.
*
2007-08-28 07:20:43 +00:00
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
2004-03-05 05:14:21 +00:00
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Principal Author: Brian Wellington
2012-03-08 00:21:15 +11:00
* $Id$
*/
#ifdef OPENSSL
#include <config.h>
2000-06-09 22:32:20 +00:00
#include <isc/entropy.h>
#include <isc/mem.h>
#include <isc/mutex.h>
#include <isc/mutexblock.h>
#include <isc/string.h>
#include <isc/thread.h>
2000-04-28 01:12:23 +00:00
#include <isc/util.h>
2009-10-05 17:30:49 +00:00
#include <dst/result.h>
#include "dst_internal.h"
#include "dst_openssl.h"
#ifdef USE_ENGINE
#include <openssl/engine.h>
#endif
static RAND_METHOD *rm = NULL;
static isc_mutex_t *locks = NULL;
static int nlocks;
#ifdef USE_ENGINE
2009-10-05 17:30:49 +00:00
static ENGINE *e = NULL;
#endif
2000-06-09 22:32:20 +00:00
static int
entropy_get(unsigned char *buf, int num) {
isc_result_t result;
if (num < 0)
return (-1);
result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
return (result == ISC_R_SUCCESS ? 1 : -1);
}
static int
entropy_status(void) {
return (dst__entropy_status() > 32);
}
static int
entropy_getpseudo(unsigned char *buf, int num) {
isc_result_t result;
if (num < 0)
return (-1);
result = dst__entropy_getdata(buf, (unsigned int) num, ISC_TRUE);
return (result == ISC_R_SUCCESS ? 1 : -1);
2000-06-09 22:32:20 +00:00
}
static void
entropy_add(const void *buf, int num, double entropy) {
/*
* Do nothing. The only call to this provides no useful data anyway.
*/
UNUSED(buf);
UNUSED(num);
UNUSED(entropy);
}
static void
lock_callback(int mode, int type, const char *file, int line) {
2000-12-04 23:39:05 +00:00
UNUSED(file);
UNUSED(line);
if ((mode & CRYPTO_LOCK) != 0)
LOCK(&locks[type]);
else
UNLOCK(&locks[type]);
}
static unsigned long
id_callback(void) {
return ((unsigned long)isc_thread_self());
}
static void *
mem_alloc(size_t size) {
2009-10-05 17:30:49 +00:00
#ifdef OPENSSL_LEAKS
void *ptr;
INSIST(dst__memory_pool != NULL);
ptr = isc_mem_allocate(dst__memory_pool, size);
return (ptr);
#else
2002-12-13 02:51:41 +00:00
INSIST(dst__memory_pool != NULL);
return (isc_mem_allocate(dst__memory_pool, size));
2009-10-05 17:30:49 +00:00
#endif
}
static void
mem_free(void *ptr) {
2002-12-13 02:51:41 +00:00
INSIST(dst__memory_pool != NULL);
if (ptr != NULL)
2002-12-13 02:51:41 +00:00
isc_mem_free(dst__memory_pool, ptr);
}
static void *
mem_realloc(void *ptr, size_t size) {
2009-10-05 17:30:49 +00:00
#ifdef OPENSSL_LEAKS
void *rptr;
INSIST(dst__memory_pool != NULL);
rptr = isc_mem_reallocate(dst__memory_pool, ptr, size);
return (rptr);
#else
2002-12-13 02:51:41 +00:00
INSIST(dst__memory_pool != NULL);
return (isc_mem_reallocate(dst__memory_pool, ptr, size));
2009-10-05 17:30:49 +00:00
#endif
}
2000-06-09 22:32:20 +00:00
isc_result_t
2009-10-05 17:30:49 +00:00
dst__openssl_init(const char *engine) {
isc_result_t result;
2008-03-31 14:42:51 +00:00
#ifdef USE_ENGINE
ENGINE *re;
2009-10-05 17:30:49 +00:00
#else
UNUSED(engine);
2008-03-31 14:42:51 +00:00
#endif
#ifdef DNS_CRYPTO_LEAKS
CRYPTO_malloc_debug_init();
CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
#endif
CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
nlocks = CRYPTO_num_locks();
locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
if (locks == NULL)
return (ISC_R_NOMEMORY);
result = isc_mutexblock_init(locks, nlocks);
if (result != ISC_R_SUCCESS)
goto cleanup_mutexalloc;
CRYPTO_set_locking_callback(lock_callback);
CRYPTO_set_id_callback(id_callback);
rm = mem_alloc(sizeof(RAND_METHOD));
if (rm == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_mutexinit;
}
rm->seed = NULL;
rm->bytes = entropy_get;
rm->cleanup = NULL;
rm->add = entropy_add;
rm->pseudorand = entropy_getpseudo;
rm->status = entropy_status;
2009-10-05 17:30:49 +00:00
#ifdef USE_ENGINE
2008-03-31 14:42:51 +00:00
OPENSSL_config(NULL);
2009-10-05 17:30:49 +00:00
if (engine != NULL && *engine == '\0')
engine = NULL;
if (engine != NULL) {
e = ENGINE_by_id(engine);
2008-03-31 14:42:51 +00:00
if (e == NULL) {
2009-10-05 17:30:49 +00:00
result = DST_R_NOENGINE;
2008-03-31 14:42:51 +00:00
goto cleanup_rm;
}
2009-10-05 17:30:49 +00:00
/* This will init the engine. */
if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
result = DST_R_NOENGINE;
2008-03-31 14:42:51 +00:00
goto cleanup_rm;
}
}
2009-10-05 17:30:49 +00:00
2008-03-31 14:42:51 +00:00
re = ENGINE_get_default_RAND();
if (re == NULL) {
re = ENGINE_new();
if (re == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_rm;
}
ENGINE_set_RAND(re, rm);
ENGINE_set_default_RAND(re);
ENGINE_free(re);
} else
ENGINE_finish(re);
#else
RAND_set_rand_method(rm);
#endif /* USE_ENGINE */
2000-06-09 22:32:20 +00:00
return (ISC_R_SUCCESS);
#ifdef USE_ENGINE
cleanup_rm:
2009-10-05 17:30:49 +00:00
if (e != NULL)
ENGINE_free(e);
e = NULL;
mem_free(rm);
2009-10-05 17:30:49 +00:00
rm = NULL;
#endif
cleanup_mutexinit:
CRYPTO_set_locking_callback(NULL);
DESTROYMUTEXBLOCK(locks, nlocks);
cleanup_mutexalloc:
mem_free(locks);
2009-10-05 17:30:49 +00:00
locks = NULL;
return (result);
2000-06-09 22:32:20 +00:00
}
void
2002-12-13 02:51:41 +00:00
dst__openssl_destroy() {
/*
* Sequence taken from apps_shutdown() in <apps/apps.h>.
*/
2009-10-05 17:30:49 +00:00
if (rm != NULL) {
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
RAND_cleanup();
#endif
mem_free(rm);
rm = NULL;
}
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
2009-10-05 17:30:49 +00:00
CONF_modules_free();
#endif
2009-10-05 17:30:49 +00:00
OBJ_cleanup();
EVP_cleanup();
2008-03-31 14:42:51 +00:00
#if defined(USE_ENGINE)
2009-10-05 17:30:49 +00:00
if (e != NULL)
ENGINE_free(e);
e = NULL;
#if defined(USE_ENGINE) && OPENSSL_VERSION_NUMBER >= 0x00907000L
ENGINE_cleanup();
#endif
2008-03-31 14:42:51 +00:00
#endif
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
CRYPTO_cleanup_all_ex_data();
#endif
ERR_clear_error();
ERR_remove_state(0);
2009-10-05 17:30:49 +00:00
ERR_free_strings();
#ifdef DNS_CRYPTO_LEAKS
CRYPTO_mem_leaks_fp(stderr);
#endif
if (locks != NULL) {
CRYPTO_set_locking_callback(NULL);
DESTROYMUTEXBLOCK(locks, nlocks);
mem_free(locks);
2009-10-05 17:30:49 +00:00
locks = NULL;
}
}
isc_result_t
dst__openssl_toresult(isc_result_t fallback) {
isc_result_t result = fallback;
int err = ERR_get_error();
switch (ERR_GET_REASON(err)) {
case ERR_R_MALLOC_FAILURE:
result = ISC_R_NOMEMORY;
break;
default:
break;
}
ERR_clear_error();
return (result);
}
#if defined(USE_ENGINE)
2008-03-31 14:42:51 +00:00
ENGINE *
2009-10-05 17:30:49 +00:00
dst__openssl_getengine(const char *engine) {
2008-03-31 14:42:51 +00:00
2009-10-05 17:30:49 +00:00
if (engine == NULL)
return (NULL);
2008-03-31 14:42:51 +00:00
if (e == NULL)
2009-10-05 17:30:49 +00:00
return (NULL);
if (strcmp(engine, ENGINE_get_id(e)) == 0)
return (e);
return (NULL);
2008-03-31 14:42:51 +00:00
}
#endif
2008-03-31 14:42:51 +00:00
#else /* OPENSSL */
#include <isc/util.h>
2002-12-13 02:51:41 +00:00
EMPTY_TRANSLATION_UNIT
#endif /* OPENSSL */
/*! \file */