2 * Portions Copyright (C) 2004, 2006 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 1999-2003 Internet Software Consortium.
4 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
13 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
16 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Principal Author: Brian Wellington
21 * $Id: openssl_link.c,v 1.1.4.3 2006/05/23 23:51:03 marka Exp $
27 #include <isc/entropy.h>
29 #include <isc/mutex.h>
30 #include <isc/mutexblock.h>
31 #include <isc/string.h>
32 #include <isc/thread.h>
35 #include "dst_internal.h"
36 #include "dst_openssl.h"
38 #include <openssl/err.h>
39 #include <openssl/rand.h>
40 #include <openssl/crypto.h>
42 #if defined(CRYPTO_LOCK_ENGINE) && (OPENSSL_VERSION_NUMBER != 0x00907000L)
47 #include <openssl/engine.h>
50 static RAND_METHOD *rm = NULL;
51 static isc_mutex_t *locks = NULL;
60 entropy_get(unsigned char *buf, int num) {
64 result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
65 return (result == ISC_R_SUCCESS ? num : -1);
69 entropy_getpseudo(unsigned char *buf, int num) {
73 result = dst__entropy_getdata(buf, (unsigned int) num, ISC_TRUE);
74 return (result == ISC_R_SUCCESS ? num : -1);
78 entropy_add(const void *buf, int num, double entropy) {
80 * Do nothing. The only call to this provides no useful data anyway.
88 lock_callback(int mode, int type, const char *file, int line) {
91 if ((mode & CRYPTO_LOCK) != 0)
99 return ((unsigned long)isc_thread_self());
103 mem_alloc(size_t size) {
104 INSIST(dst__memory_pool != NULL);
105 return (isc_mem_allocate(dst__memory_pool, size));
109 mem_free(void *ptr) {
110 INSIST(dst__memory_pool != NULL);
112 isc_mem_free(dst__memory_pool, ptr);
116 mem_realloc(void *ptr, size_t size) {
119 INSIST(dst__memory_pool != NULL);
123 if (p != NULL && ptr != NULL)
124 memcpy(p, ptr, size);
132 dst__openssl_init() {
135 CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
136 nlocks = CRYPTO_num_locks();
137 locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
139 return (ISC_R_NOMEMORY);
140 result = isc_mutexblock_init(locks, nlocks);
141 if (result != ISC_R_SUCCESS)
142 goto cleanup_mutexalloc;
143 CRYPTO_set_locking_callback(lock_callback);
144 CRYPTO_set_id_callback(id_callback);
145 rm = mem_alloc(sizeof(RAND_METHOD));
147 result = ISC_R_NOMEMORY;
148 goto cleanup_mutexinit;
151 rm->bytes = entropy_get;
153 rm->add = entropy_add;
154 rm->pseudorand = entropy_getpseudo;
159 result = ISC_R_NOMEMORY;
162 ENGINE_set_RAND(e, rm);
163 RAND_set_rand_method(rm);
165 RAND_set_rand_method(rm);
167 return (ISC_R_SUCCESS);
174 DESTROYMUTEXBLOCK(locks, nlocks);
181 dst__openssl_destroy() {
190 DESTROYMUTEXBLOCK(locks, nlocks);
198 dst__openssl_toresult(isc_result_t fallback) {
199 isc_result_t result = fallback;
200 int err = ERR_get_error();
202 switch (ERR_GET_REASON(err)) {
203 case ERR_R_MALLOC_FAILURE:
204 result = ISC_R_NOMEMORY;
215 #include <isc/util.h>
217 EMPTY_TRANSLATION_UNIT