openssl_link.c revision 1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6
63af1a646a006867dbd99dc0aa74559d832a420fMark Andrews/*
63af1a646a006867dbd99dc0aa74559d832a420fMark Andrews * Portions Copyright (C) 1999-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
63af1a646a006867dbd99dc0aa74559d832a420fMark Andrews *
60988462e5d6db53205851d056e3482a29239be9Evan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM AND
60988462e5d6db53205851d056e3482a29239be9Evan Hunt * NETWORK ASSOCIATES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
60988462e5d6db53205851d056e3482a29239be9Evan Hunt * SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
d58e33bfabfee19a035031dac633d36659738d56Evan Hunt * FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE CONSORTIUM OR NETWORK
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * ASSOCIATES BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
60988462e5d6db53205851d056e3482a29239be9Evan Hunt * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
60988462e5d6db53205851d056e3482a29239be9Evan Hunt * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * PERFORMANCE OF THIS SOFTWARE.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Principal Author: Brian Wellington
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * $Id: openssl_link.c,v 1.49 2001/11/30 01:59:31 gson Exp $
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef OPENSSL
63af1a646a006867dbd99dc0aa74559d832a420fMark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#include <config.h>
63af1a646a006867dbd99dc0aa74559d832a420fMark Andrews
#include <isc/entropy.h>
#include <isc/mem.h>
#include <isc/mutex.h>
#include <isc/mutexblock.h>
#include <isc/string.h>
#include <isc/thread.h>
#include <isc/util.h>
#include "dst_internal.h"
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
#ifdef CRYPTO_LOCK_ENGINE
#include <openssl/engine.h>
#endif
static RAND_METHOD *rm = NULL;
static isc_mutex_t *locks = NULL;
static int nlocks;
#ifdef CRYPTO_LOCK_ENGINE
static ENGINE *e;
#endif
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 ? 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);
}
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) {
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) {
INSIST(dst__memory_pool != NULL);
return (isc_mem_allocate(dst__memory_pool, size));
}
static void
mem_free(void *ptr) {
INSIST(dst__memory_pool != NULL);
if (ptr != NULL)
isc_mem_free(dst__memory_pool, ptr);
}
static void *
mem_realloc(void *ptr, size_t size) {
void *p;
INSIST(dst__memory_pool != NULL);
p = NULL;
if (size > 0) {
p = mem_alloc(size);
if (p != NULL && ptr != NULL)
memcpy(p, ptr, size);
}
if (ptr != NULL)
mem_free(ptr);
return (p);
}
isc_result_t
dst__openssl_init() {
isc_result_t result;
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 = NULL;
#ifdef CRYPTO_LOCK_ENGINE
e = ENGINE_new();
if (e == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_rm;
}
ENGINE_set_RAND(e, rm);
RAND_set_rand_method(e);
#else
RAND_set_rand_method(rm);
#endif
return (ISC_R_SUCCESS);
#ifdef CRYPTO_LOCK_ENGINE
cleanup_rm:
mem_free(rm);
#endif
cleanup_mutexinit:
DESTROYMUTEXBLOCK(locks, nlocks);
cleanup_mutexalloc:
mem_free(locks);
return (result);
}
void
dst__openssl_destroy() {
ERR_clear_error();
#ifdef CRYPTO_LOCK_ENGINE
if (e != NULL)
ENGINE_free(e);
#endif
if (locks != NULL) {
DESTROYMUTEXBLOCK(locks, nlocks);
mem_free(locks);
}
if (rm != NULL)
mem_free(rm);
}
#else /* OPENSSL */
#include <isc/util.h>
EMPTY_TRANSLATION_UNIT
#endif /* OPENSSL */