2 * Portions Copyright (C) 2004-2011 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
12 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
24 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
26 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 * Principal Author: Brian Wellington
34 * $Id: openssl_link.c,v 1.29.54.2 2011-03-12 04:59:17 tbox Exp $
40 #include <isc/entropy.h>
42 #include <isc/mutex.h>
43 #include <isc/mutexblock.h>
44 #include <isc/string.h>
45 #include <isc/thread.h>
48 #include <dst/result.h>
50 #include "dst_internal.h"
51 #include "dst_openssl.h"
54 #include <openssl/engine.h>
57 static RAND_METHOD *rm = NULL;
59 static isc_mutex_t *locks = NULL;
63 static ENGINE *e = NULL;
67 entropy_get(unsigned char *buf, int num) {
71 result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
72 return (result == ISC_R_SUCCESS ? 1 : -1);
76 entropy_status(void) {
77 return (dst__entropy_status() > 32);
81 entropy_getpseudo(unsigned char *buf, int num) {
85 result = dst__entropy_getdata(buf, (unsigned int) num, ISC_TRUE);
86 return (result == ISC_R_SUCCESS ? 1 : -1);
90 entropy_add(const void *buf, int num, double entropy) {
92 * Do nothing. The only call to this provides no useful data anyway.
100 lock_callback(int mode, int type, const char *file, int line) {
103 if ((mode & CRYPTO_LOCK) != 0)
106 UNLOCK(&locks[type]);
111 return ((unsigned long)isc_thread_self());
115 mem_alloc(size_t size) {
119 INSIST(dst__memory_pool != NULL);
120 ptr = isc_mem_allocate(dst__memory_pool, size);
123 INSIST(dst__memory_pool != NULL);
124 return (isc_mem_allocate(dst__memory_pool, size));
129 mem_free(void *ptr) {
130 INSIST(dst__memory_pool != NULL);
132 isc_mem_free(dst__memory_pool, ptr);
136 mem_realloc(void *ptr, size_t size) {
140 INSIST(dst__memory_pool != NULL);
141 rptr = isc_mem_reallocate(dst__memory_pool, ptr, size);
144 INSIST(dst__memory_pool != NULL);
145 return (isc_mem_reallocate(dst__memory_pool, ptr, size));
150 dst__openssl_init(const char *engine) {
159 #ifdef DNS_CRYPTO_LEAKS
160 CRYPTO_malloc_debug_init();
161 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
162 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
164 CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
165 nlocks = CRYPTO_num_locks();
166 locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
168 return (ISC_R_NOMEMORY);
169 result = isc_mutexblock_init(locks, nlocks);
170 if (result != ISC_R_SUCCESS)
171 goto cleanup_mutexalloc;
172 CRYPTO_set_locking_callback(lock_callback);
173 CRYPTO_set_id_callback(id_callback);
175 rm = mem_alloc(sizeof(RAND_METHOD));
177 result = ISC_R_NOMEMORY;
178 goto cleanup_mutexinit;
181 rm->bytes = entropy_get;
183 rm->add = entropy_add;
184 rm->pseudorand = entropy_getpseudo;
185 rm->status = entropy_status;
188 OPENSSL_config(NULL);
190 if (engine != NULL && *engine == '\0')
193 if (engine != NULL) {
194 e = ENGINE_by_id(engine);
196 result = DST_R_NOENGINE;
199 /* This will init the engine. */
200 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
201 result = DST_R_NOENGINE;
206 re = ENGINE_get_default_RAND();
210 result = ISC_R_NOMEMORY;
213 ENGINE_set_RAND(re, rm);
214 ENGINE_set_default_RAND(re);
219 RAND_set_rand_method(rm);
220 #endif /* USE_ENGINE */
221 return (ISC_R_SUCCESS);
232 CRYPTO_set_locking_callback(NULL);
233 DESTROYMUTEXBLOCK(locks, nlocks);
241 dst__openssl_destroy() {
244 * Sequence taken from apps_shutdown() in <apps/apps.h>.
247 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
253 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
258 #if defined(USE_ENGINE)
262 #if defined(USE_ENGINE) && OPENSSL_VERSION_NUMBER >= 0x00907000L
266 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
267 CRYPTO_cleanup_all_ex_data();
273 #ifdef DNS_CRYPTO_LEAKS
274 CRYPTO_mem_leaks_fp(stderr);
278 CRYPTO_set_locking_callback(NULL);
279 DESTROYMUTEXBLOCK(locks, nlocks);
286 dst__openssl_toresult(isc_result_t fallback) {
287 isc_result_t result = fallback;
288 int err = ERR_get_error();
290 switch (ERR_GET_REASON(err)) {
291 case ERR_R_MALLOC_FAILURE:
292 result = ISC_R_NOMEMORY;
301 #if defined(USE_ENGINE)
303 dst__openssl_getengine(const char *engine) {
309 if (strcmp(engine, ENGINE_get_id(e)) == 0)
317 #include <isc/util.h>
319 EMPTY_TRANSLATION_UNIT