1999-07-12 20:08:42 +00:00
|
|
|
/*
|
2005-04-29 00:24:12 +00:00
|
|
|
* Portions Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
|
2004-03-05 05:14:21 +00:00
|
|
|
* Portions Copyright (C) 1999-2003 Internet Software Consortium.
|
2000-06-09 20:58:39 +00:00
|
|
|
* Portions Copyright (C) 1995-2000 by Network Associates, Inc.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-05-08 14:38:29 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
1999-07-12 20:08:42 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
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.
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Principal Author: Brian Wellington
|
2005-06-17 03:55:51 +00:00
|
|
|
* $Id: openssl_link.c,v 1.5 2005/06/17 03:55:51 marka Exp $
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2001-07-10 04:01:19 +00:00
|
|
|
#ifdef OPENSSL
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2000-06-09 22:32:20 +00:00
|
|
|
#include <isc/entropy.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
2000-12-04 23:06:37 +00:00
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/mutexblock.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-12-04 23:06:37 +00:00
|
|
|
#include <isc/thread.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
#include "dst_internal.h"
|
2002-03-19 04:30:57 +00:00
|
|
|
#include "dst_openssl.h"
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2001-11-07 23:03:54 +00:00
|
|
|
#include <openssl/err.h>
|
2000-06-09 22:32:20 +00:00
|
|
|
#include <openssl/rand.h>
|
2005-06-17 03:55:51 +00:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/conf.h>
|
2001-02-14 20:26:48 +00:00
|
|
|
#include <openssl/crypto.h>
|
2001-02-14 20:57:15 +00:00
|
|
|
|
2002-10-31 04:35:02 +00:00
|
|
|
#if defined(CRYPTO_LOCK_ENGINE) && (OPENSSL_VERSION_NUMBER < 0x00907000L)
|
|
|
|
#define USE_ENGINE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_ENGINE
|
2001-02-14 20:57:15 +00:00
|
|
|
#include <openssl/engine.h>
|
|
|
|
#endif
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-09 23:31:55 +00:00
|
|
|
static RAND_METHOD *rm = NULL;
|
2001-02-14 20:26:48 +00:00
|
|
|
static isc_mutex_t *locks = NULL;
|
|
|
|
static int nlocks;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2002-10-31 04:35:02 +00:00
|
|
|
#ifdef USE_ENGINE
|
2001-02-14 20:57:15 +00:00
|
|
|
static ENGINE *e;
|
|
|
|
#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);
|
2000-06-09 23:31:55 +00:00
|
|
|
result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
|
|
|
|
return (result == ISC_R_SUCCESS ? num : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ? num : -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);
|
|
|
|
}
|
|
|
|
|
2000-12-04 23:06:37 +00:00
|
|
|
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);
|
2000-12-04 23:06:37 +00:00
|
|
|
if ((mode & CRYPTO_LOCK) != 0)
|
|
|
|
LOCK(&locks[type]);
|
|
|
|
else
|
|
|
|
UNLOCK(&locks[type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
id_callback(void) {
|
|
|
|
return ((unsigned long)isc_thread_self());
|
|
|
|
}
|
|
|
|
|
2001-11-07 23:03:54 +00:00
|
|
|
static void *
|
|
|
|
mem_alloc(size_t size) {
|
2002-12-13 02:51:41 +00:00
|
|
|
INSIST(dst__memory_pool != NULL);
|
|
|
|
return (isc_mem_allocate(dst__memory_pool, size));
|
2001-11-07 23:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mem_free(void *ptr) {
|
2002-12-13 02:51:41 +00:00
|
|
|
INSIST(dst__memory_pool != NULL);
|
2001-11-07 23:03:54 +00:00
|
|
|
if (ptr != NULL)
|
2002-12-13 02:51:41 +00:00
|
|
|
isc_mem_free(dst__memory_pool, ptr);
|
2001-11-07 23:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
mem_realloc(void *ptr, size_t size) {
|
|
|
|
void *p;
|
|
|
|
|
2002-12-13 02:51:41 +00:00
|
|
|
INSIST(dst__memory_pool != NULL);
|
2001-11-07 23:03:54 +00:00
|
|
|
p = NULL;
|
2004-03-18 02:58:08 +00:00
|
|
|
if (size > 0U) {
|
2001-11-07 23:03:54 +00:00
|
|
|
p = mem_alloc(size);
|
|
|
|
if (p != NULL && ptr != NULL)
|
|
|
|
memcpy(p, ptr, size);
|
|
|
|
}
|
|
|
|
if (ptr != NULL)
|
|
|
|
mem_free(ptr);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2000-06-09 22:32:20 +00:00
|
|
|
isc_result_t
|
2002-12-13 02:51:41 +00:00
|
|
|
dst__openssl_init() {
|
2000-12-04 23:06:37 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
2005-06-17 02:22:45 +00:00
|
|
|
#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
|
2001-11-07 23:03:54 +00:00
|
|
|
CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
|
2001-02-14 20:26:48 +00:00
|
|
|
nlocks = CRYPTO_num_locks();
|
2001-11-07 23:03:54 +00:00
|
|
|
locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
|
2001-02-14 20:26:48 +00:00
|
|
|
if (locks == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
result = isc_mutexblock_init(locks, nlocks);
|
2001-02-14 20:57:15 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_mutexalloc;
|
2000-12-04 23:06:37 +00:00
|
|
|
CRYPTO_set_locking_callback(lock_callback);
|
|
|
|
CRYPTO_set_id_callback(id_callback);
|
2001-11-07 23:03:54 +00:00
|
|
|
rm = mem_alloc(sizeof(RAND_METHOD));
|
2001-02-14 20:57:15 +00:00
|
|
|
if (rm == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto cleanup_mutexinit;
|
|
|
|
}
|
2000-06-09 23:31:55 +00:00
|
|
|
rm->seed = NULL;
|
|
|
|
rm->bytes = entropy_get;
|
|
|
|
rm->cleanup = NULL;
|
|
|
|
rm->add = entropy_add;
|
|
|
|
rm->pseudorand = entropy_getpseudo;
|
|
|
|
rm->status = NULL;
|
2002-10-31 04:35:02 +00:00
|
|
|
#ifdef USE_ENGINE
|
2001-02-14 20:57:15 +00:00
|
|
|
e = ENGINE_new();
|
|
|
|
if (e == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto cleanup_rm;
|
|
|
|
}
|
|
|
|
ENGINE_set_RAND(e, rm);
|
|
|
|
RAND_set_rand_method(e);
|
|
|
|
#else
|
2000-06-09 23:31:55 +00:00
|
|
|
RAND_set_rand_method(rm);
|
2001-02-14 20:57:15 +00:00
|
|
|
#endif
|
2000-06-09 22:32:20 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2001-02-14 20:57:15 +00:00
|
|
|
|
2002-10-31 04:35:02 +00:00
|
|
|
#ifdef USE_ENGINE
|
2001-02-14 20:57:15 +00:00
|
|
|
cleanup_rm:
|
2001-11-07 23:03:54 +00:00
|
|
|
mem_free(rm);
|
2001-02-14 20:57:15 +00:00
|
|
|
#endif
|
|
|
|
cleanup_mutexinit:
|
2001-11-30 01:59:49 +00:00
|
|
|
DESTROYMUTEXBLOCK(locks, nlocks);
|
2001-02-14 20:57:15 +00:00
|
|
|
cleanup_mutexalloc:
|
2001-11-07 23:03:54 +00:00
|
|
|
mem_free(locks);
|
2001-02-14 20:57:15 +00:00
|
|
|
return (result);
|
2000-06-09 22:32:20 +00:00
|
|
|
}
|
|
|
|
|
2000-06-09 23:31:55 +00:00
|
|
|
void
|
2002-12-13 02:51:41 +00:00
|
|
|
dst__openssl_destroy() {
|
2005-06-17 02:22:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sequence taken from apps_shutdown() in <apps/apps.h>.
|
|
|
|
*/
|
|
|
|
CONF_modules_unload(1);
|
|
|
|
EVP_cleanup();
|
|
|
|
#ifdef USE_ENGINE
|
|
|
|
ENGINE_cleanup();
|
|
|
|
#endif
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
ERR_clear_error();
|
|
|
|
ERR_free_strings();
|
|
|
|
ERR_remove_state(0);
|
|
|
|
|
|
|
|
#ifdef DNS_CRYPTO_LEAKS
|
|
|
|
CRYPTO_mem_leaks_fp(stderr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* The old error sequence that leaked. Remove for 9.4.1 if
|
|
|
|
* there are no issues by then.
|
|
|
|
*/
|
2001-11-07 23:03:54 +00:00
|
|
|
ERR_clear_error();
|
2002-10-31 04:35:02 +00:00
|
|
|
#ifdef USE_ENGINE
|
2003-02-18 06:25:11 +00:00
|
|
|
if (e != NULL) {
|
2001-11-07 23:03:54 +00:00
|
|
|
ENGINE_free(e);
|
2003-02-18 06:25:11 +00:00
|
|
|
e = NULL;
|
|
|
|
}
|
2005-06-17 02:22:45 +00:00
|
|
|
#endif
|
2001-11-07 23:03:54 +00:00
|
|
|
#endif
|
2001-02-14 20:26:48 +00:00
|
|
|
if (locks != NULL) {
|
2001-11-30 01:59:49 +00:00
|
|
|
DESTROYMUTEXBLOCK(locks, nlocks);
|
2001-11-07 23:03:54 +00:00
|
|
|
mem_free(locks);
|
2001-02-14 20:26:48 +00:00
|
|
|
}
|
2005-06-17 02:22:45 +00:00
|
|
|
if (rm != NULL) {
|
|
|
|
RAND_cleanup();
|
2001-11-07 23:03:54 +00:00
|
|
|
mem_free(rm);
|
2005-06-17 02:22:45 +00:00
|
|
|
}
|
2000-06-09 23:31:55 +00:00
|
|
|
}
|
|
|
|
|
2002-03-19 04:30:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2001-11-20 21:28:41 +00:00
|
|
|
#else /* OPENSSL */
|
|
|
|
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2002-12-13 02:51:41 +00:00
|
|
|
EMPTY_TRANSLATION_UNIT
|
2001-11-20 21:28:41 +00:00
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
#endif /* OPENSSL */
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|