2 * Portions Copyright (C) 2004-2012 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
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>
50 #include <dst/result.h>
52 #include "dst_internal.h"
53 #include "dst_openssl.h"
56 #include <openssl/engine.h>
59 static RAND_METHOD *rm = NULL;
61 static isc_mutex_t *locks = NULL;
65 static ENGINE *e = NULL;
69 entropy_get(unsigned char *buf, int num) {
73 result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
74 return (result == ISC_R_SUCCESS ? 1 : -1);
78 entropy_status(void) {
79 return (dst__entropy_status() > 32);
83 entropy_getpseudo(unsigned char *buf, int num) {
87 result = dst__entropy_getdata(buf, (unsigned int) num, ISC_TRUE);
88 return (result == ISC_R_SUCCESS ? 1 : -1);
92 entropy_add(const void *buf, int num, double entropy) {
94 * Do nothing. The only call to this provides no useful data anyway.
102 lock_callback(int mode, int type, const char *file, int line) {
105 if ((mode & CRYPTO_LOCK) != 0)
108 UNLOCK(&locks[type]);
113 return ((unsigned long)isc_thread_self());
117 mem_alloc(size_t size) {
121 INSIST(dst__memory_pool != NULL);
122 ptr = isc_mem_allocate(dst__memory_pool, size);
125 INSIST(dst__memory_pool != NULL);
126 return (isc_mem_allocate(dst__memory_pool, size));
131 mem_free(void *ptr) {
132 INSIST(dst__memory_pool != NULL);
134 isc_mem_free(dst__memory_pool, ptr);
138 mem_realloc(void *ptr, size_t size) {
142 INSIST(dst__memory_pool != NULL);
143 rptr = isc_mem_reallocate(dst__memory_pool, ptr, size);
146 INSIST(dst__memory_pool != NULL);
147 return (isc_mem_reallocate(dst__memory_pool, ptr, size));
152 dst__openssl_init(const char *engine) {
161 #ifdef DNS_CRYPTO_LEAKS
162 CRYPTO_malloc_debug_init();
163 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
164 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
166 CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
167 nlocks = CRYPTO_num_locks();
168 locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
170 return (ISC_R_NOMEMORY);
171 result = isc_mutexblock_init(locks, nlocks);
172 if (result != ISC_R_SUCCESS)
173 goto cleanup_mutexalloc;
174 CRYPTO_set_locking_callback(lock_callback);
175 CRYPTO_set_id_callback(id_callback);
177 ERR_load_crypto_strings();
179 rm = mem_alloc(sizeof(RAND_METHOD));
181 result = ISC_R_NOMEMORY;
182 goto cleanup_mutexinit;
185 rm->bytes = entropy_get;
187 rm->add = entropy_add;
188 rm->pseudorand = entropy_getpseudo;
189 rm->status = entropy_status;
192 OPENSSL_config(NULL);
194 if (engine != NULL && *engine == '\0')
197 if (engine != NULL) {
198 e = ENGINE_by_id(engine);
200 result = DST_R_NOENGINE;
203 /* This will init the engine. */
204 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
205 result = DST_R_NOENGINE;
210 re = ENGINE_get_default_RAND();
214 result = ISC_R_NOMEMORY;
217 ENGINE_set_RAND(re, rm);
218 ENGINE_set_default_RAND(re);
223 RAND_set_rand_method(rm);
224 #endif /* USE_ENGINE */
225 return (ISC_R_SUCCESS);
236 CRYPTO_set_locking_callback(NULL);
237 DESTROYMUTEXBLOCK(locks, nlocks);
245 dst__openssl_destroy() {
248 * Sequence taken from apps_shutdown() in <apps/apps.h>.
251 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
257 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
262 #if defined(USE_ENGINE)
266 #if defined(USE_ENGINE) && OPENSSL_VERSION_NUMBER >= 0x00907000L
270 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
271 CRYPTO_cleanup_all_ex_data();
277 #ifdef DNS_CRYPTO_LEAKS
278 CRYPTO_mem_leaks_fp(stderr);
282 CRYPTO_set_locking_callback(NULL);
283 DESTROYMUTEXBLOCK(locks, nlocks);
290 dst__openssl_toresult(isc_result_t fallback) {
291 isc_result_t result = fallback;
292 unsigned long err = ERR_get_error();
294 switch (ERR_GET_REASON(err)) {
295 case ERR_R_MALLOC_FAILURE:
296 result = ISC_R_NOMEMORY;
306 dst__openssl_toresult2(const char *funcname, isc_result_t fallback) {
307 isc_result_t result = fallback;
308 unsigned long err = ERR_peek_error();
309 const char *file, *data;
313 switch (ERR_GET_REASON(err)) {
314 case ERR_R_MALLOC_FAILURE:
315 result = ISC_R_NOMEMORY;
320 isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
321 DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING,
322 "%s failed", funcname);
324 err = ERR_get_error_line_data(&file, &line, &data, &flags);
327 ERR_error_string_n(err, buf, sizeof(buf));
328 isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
329 DNS_LOGMODULE_CRYPTO, ISC_LOG_INFO,
330 "%s:%s:%d:%s", buf, file, line,
331 (flags & ERR_TXT_STRING) ? data : "");
339 #if defined(USE_ENGINE)
341 dst__openssl_getengine(const char *engine) {
347 if (strcmp(engine, ENGINE_get_id(e)) == 0)
355 #include <isc/util.h>
357 EMPTY_TRANSLATION_UNIT