openssl_link.c 7.41 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Portions Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Portions Copyright (C) 1999-2003  Internet Software Consortium.
Automatic Updater's avatar
Automatic Updater committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * 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.
 *
17
 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
18
 *
Automatic Updater's avatar
Automatic Updater committed
19
 * Permission to use, copy, modify, and/or distribute this software for any
20 21
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
22
 *
Mark Andrews's avatar
Mark Andrews committed
23 24 25 26 27 28 29
 * 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.
30 31 32 33
 */

/*
 * Principal Author: Brian Wellington
34
 * $Id: openssl_link.c,v 1.28 2010/09/15 03:06:15 marka Exp $
35
 */
36
#ifdef OPENSSL
37 38 39

#include <config.h>

Brian Wellington's avatar
Brian Wellington committed
40
#include <isc/entropy.h>
41
#include <isc/mem.h>
42 43
#include <isc/mutex.h>
#include <isc/mutexblock.h>
44
#include <isc/string.h>
45
#include <isc/thread.h>
Bob Halley's avatar
Bob Halley committed
46
#include <isc/util.h>
47

Francis Dupont's avatar
Francis Dupont committed
48 49
#include <dst/result.h>

50
#include "dst_internal.h"
51
#include "dst_openssl.h"
52

53
#include <openssl/err.h>
Brian Wellington's avatar
Brian Wellington committed
54
#include <openssl/rand.h>
55 56
#include <openssl/evp.h>
#include <openssl/conf.h>
57
#include <openssl/crypto.h>
58

59
#if defined(CRYPTO_LOCK_ENGINE) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
60 61 62 63
#define USE_ENGINE 1
#endif

#ifdef USE_ENGINE
64 65
#include <openssl/engine.h>
#endif
66

67
static RAND_METHOD *rm = NULL;
68

69 70
static isc_mutex_t *locks = NULL;
static int nlocks;
71

72
#ifdef USE_ENGINE
Francis Dupont's avatar
Francis Dupont committed
73
static ENGINE *e = NULL;
74 75
#endif

Brian Wellington's avatar
Brian Wellington committed
76 77 78 79 80
static int
entropy_get(unsigned char *buf, int num) {
	isc_result_t result;
	if (num < 0)
		return (-1);
81
	result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
82
	return (result == ISC_R_SUCCESS ? 1 : -1);
83 84
}

85 86 87 88 89
static int
entropy_status(void) {
	return (dst__entropy_status() > 32);
}

90 91 92 93 94 95
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);
96
	return (result == ISC_R_SUCCESS ? 1 : -1);
Brian Wellington's avatar
Brian Wellington committed
97 98 99 100 101 102 103 104 105 106 107 108
}

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);
}

109 110
static void
lock_callback(int mode, int type, const char *file, int line) {
111 112
	UNUSED(file);
	UNUSED(line);
113 114 115 116 117 118 119 120 121 122 123
	if ((mode & CRYPTO_LOCK) != 0)
		LOCK(&locks[type]);
	else
		UNLOCK(&locks[type]);
}

static unsigned long
id_callback(void) {
	return ((unsigned long)isc_thread_self());
}

124 125
static void *
mem_alloc(size_t size) {
Francis Dupont's avatar
Francis Dupont committed
126 127 128 129 130 131 132
#ifdef OPENSSL_LEAKS
	void *ptr;

	INSIST(dst__memory_pool != NULL);
	ptr = isc_mem_allocate(dst__memory_pool, size);
	return (ptr);
#else
Mark Andrews's avatar
Mark Andrews committed
133 134
	INSIST(dst__memory_pool != NULL);
	return (isc_mem_allocate(dst__memory_pool, size));
Francis Dupont's avatar
Francis Dupont committed
135
#endif
136 137 138 139
}

static void
mem_free(void *ptr) {
Mark Andrews's avatar
Mark Andrews committed
140
	INSIST(dst__memory_pool != NULL);
141
	if (ptr != NULL)
Mark Andrews's avatar
Mark Andrews committed
142
		isc_mem_free(dst__memory_pool, ptr);
143 144 145 146
}

static void *
mem_realloc(void *ptr, size_t size) {
Francis Dupont's avatar
Francis Dupont committed
147 148 149 150 151 152 153
#ifdef OPENSSL_LEAKS
	void *rptr;

	INSIST(dst__memory_pool != NULL);
	rptr = isc_mem_reallocate(dst__memory_pool, ptr, size);
	return (rptr);
#else
Mark Andrews's avatar
Mark Andrews committed
154
	INSIST(dst__memory_pool != NULL);
155
	return (isc_mem_reallocate(dst__memory_pool, ptr, size));
Francis Dupont's avatar
Francis Dupont committed
156
#endif
157 158
}

Brian Wellington's avatar
Brian Wellington committed
159
isc_result_t
Francis Dupont's avatar
Francis Dupont committed
160
dst__openssl_init(const char *engine) {
161
	isc_result_t result;
Francis Dupont's avatar
Francis Dupont committed
162 163
#ifdef USE_ENGINE
	ENGINE *re;
Francis Dupont's avatar
Francis Dupont committed
164 165 166
#else

	UNUSED(engine);
Francis Dupont's avatar
Francis Dupont committed
167
#endif
168

169 170 171 172 173
#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
174
	CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
175
	nlocks = CRYPTO_num_locks();
176
	locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
177 178 179
	if (locks == NULL)
		return (ISC_R_NOMEMORY);
	result = isc_mutexblock_init(locks, nlocks);
180 181
	if (result != ISC_R_SUCCESS)
		goto cleanup_mutexalloc;
182 183
	CRYPTO_set_locking_callback(lock_callback);
	CRYPTO_set_id_callback(id_callback);
184

185
	rm = mem_alloc(sizeof(RAND_METHOD));
186 187 188 189
	if (rm == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup_mutexinit;
	}
190 191 192 193 194
	rm->seed = NULL;
	rm->bytes = entropy_get;
	rm->cleanup = NULL;
	rm->add = entropy_add;
	rm->pseudorand = entropy_getpseudo;
195
	rm->status = entropy_status;
Francis Dupont's avatar
Francis Dupont committed
196

197
#ifdef USE_ENGINE
Francis Dupont's avatar
Francis Dupont committed
198
	OPENSSL_config(NULL);
Francis Dupont's avatar
Francis Dupont committed
199 200 201 202 203 204

	if (engine != NULL && *engine == '\0')
		engine = NULL;

	if (engine != NULL) {
		e = ENGINE_by_id(engine);
Francis Dupont's avatar
Francis Dupont committed
205
		if (e == NULL) {
Francis Dupont's avatar
Francis Dupont committed
206
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
207 208
			goto cleanup_rm;
		}
Francis Dupont's avatar
Francis Dupont committed
209 210 211
		/* This will init the engine. */
		if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
212 213 214
			goto cleanup_rm;
		}
	}
Francis Dupont's avatar
Francis Dupont committed
215

Francis Dupont's avatar
Francis Dupont committed
216 217 218 219 220 221 222 223 224 225 226 227
	re = ENGINE_get_default_RAND();
	if (re == NULL) {
		re = ENGINE_new();
		if (re == NULL) {
			result = ISC_R_NOMEMORY;
			goto cleanup_rm;
		}
		ENGINE_set_RAND(re, rm);
		ENGINE_set_default_RAND(re);
		ENGINE_free(re);
	} else
		ENGINE_finish(re);
228
#else
229
	RAND_set_rand_method(rm);
230
#endif /* USE_ENGINE */
Brian Wellington's avatar
Brian Wellington committed
231
	return (ISC_R_SUCCESS);
232

233
#ifdef USE_ENGINE
234
 cleanup_rm:
Francis Dupont's avatar
Francis Dupont committed
235 236 237
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
238
	mem_free(rm);
Francis Dupont's avatar
Francis Dupont committed
239
	rm = NULL;
240 241
#endif
 cleanup_mutexinit:
242
	CRYPTO_set_locking_callback(NULL);
243
	DESTROYMUTEXBLOCK(locks, nlocks);
244
 cleanup_mutexalloc:
245
	mem_free(locks);
Francis Dupont's avatar
Francis Dupont committed
246
	locks = NULL;
247
	return (result);
Brian Wellington's avatar
Brian Wellington committed
248 249
}

250
void
Mark Andrews's avatar
Mark Andrews committed
251
dst__openssl_destroy() {
252 253 254 255

	/*
	 * Sequence taken from apps_shutdown() in <apps/apps.h>.
	 */
Francis Dupont's avatar
Francis Dupont committed
256 257 258 259 260 261 262
	if (rm != NULL) {
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
		RAND_cleanup();
#endif
		mem_free(rm);
		rm = NULL;
	}
263
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
Francis Dupont's avatar
Francis Dupont committed
264
	CONF_modules_free();
265
#endif
Francis Dupont's avatar
Francis Dupont committed
266
	OBJ_cleanup();
267
	EVP_cleanup();
Francis Dupont's avatar
Francis Dupont committed
268
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
269 270 271
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
272
#if defined(USE_ENGINE) && OPENSSL_VERSION_NUMBER >= 0x00907000L
273 274
	ENGINE_cleanup();
#endif
Francis Dupont's avatar
Francis Dupont committed
275
#endif
276
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
277
	CRYPTO_cleanup_all_ex_data();
278
#endif
279 280
	ERR_clear_error();
	ERR_remove_state(0);
Francis Dupont's avatar
Francis Dupont committed
281
	ERR_free_strings();
282 283 284 285 286

#ifdef  DNS_CRYPTO_LEAKS
	CRYPTO_mem_leaks_fp(stderr);
#endif

287 288 289 290
	if (locks != NULL) {
		CRYPTO_set_locking_callback(NULL);
		DESTROYMUTEXBLOCK(locks, nlocks);
		mem_free(locks);
Francis Dupont's avatar
Francis Dupont committed
291
		locks = NULL;
292
	}
293 294
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
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);
}

Francis Dupont's avatar
Francis Dupont committed
311
ENGINE *
Francis Dupont's avatar
Francis Dupont committed
312
dst__openssl_getengine(const char *engine) {
Francis Dupont's avatar
Francis Dupont committed
313

Francis Dupont's avatar
Francis Dupont committed
314 315
	if (engine == NULL)
		return (NULL);
316
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
317
	if (e == NULL)
Francis Dupont's avatar
Francis Dupont committed
318 319 320 321 322
		return (NULL);
	if (strcmp(engine, ENGINE_get_id(e)) == 0)
		return (e);
#endif
	return (NULL);
Francis Dupont's avatar
Francis Dupont committed
323 324
}

325 326 327 328
#else /* OPENSSL */

#include <isc/util.h>

Mark Andrews's avatar
Mark Andrews committed
329
EMPTY_TRANSLATION_UNIT
330

331
#endif /* OPENSSL */
332
/*! \file */