openssl_link.c 9.53 KB
Newer Older
1
/*
2
 * Portions Copyright (C) 2004-2012, 2014-2016  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
Mark Andrews's avatar
Mark Andrews committed
34
 * $Id$
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

48 49
#include <dns/log.h>

Francis Dupont's avatar
Francis Dupont committed
50 51
#include <dst/result.h>

52
#include "dst_internal.h"
53
#include "dst_openssl.h"
54

55
#ifdef USE_ENGINE
56 57
#include <openssl/engine.h>
#endif
58

59
static RAND_METHOD *rm = NULL;
60

61 62
static isc_mutex_t *locks = NULL;
static int nlocks;
63

64
#ifdef USE_ENGINE
Francis Dupont's avatar
Francis Dupont committed
65
static ENGINE *e = NULL;
66 67
#endif

Brian Wellington's avatar
Brian Wellington committed
68 69 70 71 72
static int
entropy_get(unsigned char *buf, int num) {
	isc_result_t result;
	if (num < 0)
		return (-1);
73
	result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
74
	return (result == ISC_R_SUCCESS ? 1 : -1);
75 76
}

77 78 79 80 81
static int
entropy_status(void) {
	return (dst__entropy_status() > 32);
}

82 83 84 85 86 87
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);
88
	return (result == ISC_R_SUCCESS ? 1 : -1);
Brian Wellington's avatar
Brian Wellington committed
89 90
}

91
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Brian Wellington's avatar
Brian Wellington committed
92 93 94 95 96 97 98 99 100
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);
}
101 102 103 104 105 106 107 108 109 110 111 112
#else
static int
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);
	return (1);
}
#endif
Brian Wellington's avatar
Brian Wellington committed
113

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

124
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
125 126 127 128
static unsigned long
id_callback(void) {
	return ((unsigned long)isc_thread_self());
}
129
#endif
130

131 132
static void *
mem_alloc(size_t size) {
Francis Dupont's avatar
Francis Dupont committed
133 134 135 136 137 138 139
#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
140 141
	INSIST(dst__memory_pool != NULL);
	return (isc_mem_allocate(dst__memory_pool, size));
Francis Dupont's avatar
Francis Dupont committed
142
#endif
143 144 145 146
}

static void
mem_free(void *ptr) {
Mark Andrews's avatar
Mark Andrews committed
147
	INSIST(dst__memory_pool != NULL);
148
	if (ptr != NULL)
Mark Andrews's avatar
Mark Andrews committed
149
		isc_mem_free(dst__memory_pool, ptr);
150 151 152 153
}

static void *
mem_realloc(void *ptr, size_t size) {
Francis Dupont's avatar
Francis Dupont committed
154 155 156 157 158 159 160
#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
161
	INSIST(dst__memory_pool != NULL);
162
	return (isc_mem_reallocate(dst__memory_pool, ptr, size));
Francis Dupont's avatar
Francis Dupont committed
163
#endif
164 165
}

Brian Wellington's avatar
Brian Wellington committed
166
isc_result_t
Francis Dupont's avatar
Francis Dupont committed
167
dst__openssl_init(const char *engine) {
168
	isc_result_t result;
Francis Dupont's avatar
Francis Dupont committed
169 170
#ifdef USE_ENGINE
	ENGINE *re;
Francis Dupont's avatar
Francis Dupont committed
171 172 173
#else

	UNUSED(engine);
Francis Dupont's avatar
Francis Dupont committed
174
#endif
175

176 177 178 179 180
#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
181
	CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
182
	nlocks = CRYPTO_num_locks();
183
	locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
184 185 186
	if (locks == NULL)
		return (ISC_R_NOMEMORY);
	result = isc_mutexblock_init(locks, nlocks);
187 188
	if (result != ISC_R_SUCCESS)
		goto cleanup_mutexalloc;
189
	CRYPTO_set_locking_callback(lock_callback);
190
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
191
	CRYPTO_set_id_callback(id_callback);
192
#endif
193

194 195
	ERR_load_crypto_strings();

196
	rm = mem_alloc(sizeof(RAND_METHOD));
197 198 199 200
	if (rm == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup_mutexinit;
	}
201 202 203 204 205
	rm->seed = NULL;
	rm->bytes = entropy_get;
	rm->cleanup = NULL;
	rm->add = entropy_add;
	rm->pseudorand = entropy_getpseudo;
206
	rm->status = entropy_status;
Francis Dupont's avatar
Francis Dupont committed
207

208
#ifdef USE_ENGINE
209 210 211
#if !defined(CONF_MFLAGS_DEFAULT_SECTION)
	OPENSSL_config(NULL);
#else
212 213 214 215 216 217 218 219 220 221
	/*
	 * OPENSSL_config() can only be called a single time as of
	 * 1.0.2e so do the steps individually.
	 */
	OPENSSL_load_builtin_modules();
	ENGINE_load_builtin_engines();
	ERR_clear_error();
	CONF_modules_load_file(NULL, NULL,
			       CONF_MFLAGS_DEFAULT_SECTION |
			       CONF_MFLAGS_IGNORE_MISSING_FILE);
222
#endif
Francis Dupont's avatar
Francis Dupont committed
223 224 225 226 227 228

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

	if (engine != NULL) {
		e = ENGINE_by_id(engine);
Francis Dupont's avatar
Francis Dupont committed
229
		if (e == NULL) {
Francis Dupont's avatar
Francis Dupont committed
230
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
231 232
			goto cleanup_rm;
		}
Francis Dupont's avatar
Francis Dupont committed
233 234 235
		/* This will init the engine. */
		if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
236 237 238
			goto cleanup_rm;
		}
	}
Francis Dupont's avatar
Francis Dupont committed
239

Francis Dupont's avatar
Francis Dupont committed
240 241 242 243 244 245 246 247 248 249 250 251
	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);
252
#else
253
	RAND_set_rand_method(rm);
254
#endif /* USE_ENGINE */
Brian Wellington's avatar
Brian Wellington committed
255
	return (ISC_R_SUCCESS);
256

257
#ifdef USE_ENGINE
258
 cleanup_rm:
Francis Dupont's avatar
Francis Dupont committed
259 260 261
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
262
	mem_free(rm);
Francis Dupont's avatar
Francis Dupont committed
263
	rm = NULL;
264 265
#endif
 cleanup_mutexinit:
266
	CRYPTO_set_locking_callback(NULL);
267
	DESTROYMUTEXBLOCK(locks, nlocks);
268
 cleanup_mutexalloc:
269
	mem_free(locks);
Francis Dupont's avatar
Francis Dupont committed
270
	locks = NULL;
271
	return (result);
Brian Wellington's avatar
Brian Wellington committed
272 273
}

274
void
275
dst__openssl_destroy(void) {
276 277 278
	/*
	 * Sequence taken from apps_shutdown() in <apps/apps.h>.
	 */
Francis Dupont's avatar
Francis Dupont committed
279 280 281 282 283 284 285
	if (rm != NULL) {
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
		RAND_cleanup();
#endif
		mem_free(rm);
		rm = NULL;
	}
286
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
Francis Dupont's avatar
Francis Dupont committed
287
	CONF_modules_free();
288
#endif
Francis Dupont's avatar
Francis Dupont committed
289
	OBJ_cleanup();
290
	EVP_cleanup();
Francis Dupont's avatar
Francis Dupont committed
291
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
292 293 294
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
295
#if defined(USE_ENGINE) && OPENSSL_VERSION_NUMBER >= 0x00907000L
296 297
	ENGINE_cleanup();
#endif
Francis Dupont's avatar
Francis Dupont committed
298
#endif
299
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
300
	CRYPTO_cleanup_all_ex_data();
301
#endif
302
	ERR_clear_error();
303
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
304
	ERR_remove_state(0);
305
#endif
Francis Dupont's avatar
Francis Dupont committed
306
	ERR_free_strings();
307 308 309 310 311

#ifdef  DNS_CRYPTO_LEAKS
	CRYPTO_mem_leaks_fp(stderr);
#endif

312 313 314 315
	if (locks != NULL) {
		CRYPTO_set_locking_callback(NULL);
		DESTROYMUTEXBLOCK(locks, nlocks);
		mem_free(locks);
Francis Dupont's avatar
Francis Dupont committed
316
		locks = NULL;
317
	}
318 319
}

Evan Hunt's avatar
Evan Hunt committed
320 321
static isc_result_t
toresult(isc_result_t fallback) {
322
	isc_result_t result = fallback;
323
	unsigned long err = ERR_get_error();
324
#ifdef HAVE_OPENSSL_ECDSA
Evan Hunt's avatar
Evan Hunt committed
325
	int lib = ERR_GET_LIB(err);
Evan Hunt's avatar
Evan Hunt committed
326
#endif
Evan Hunt's avatar
Evan Hunt committed
327
	int reason = ERR_GET_REASON(err);
328

Evan Hunt's avatar
Evan Hunt committed
329 330 331 332 333
	switch (reason) {
	/*
	 * ERR_* errors are globally unique; others
	 * are unique per sublibrary
	 */
334 335 336 337
	case ERR_R_MALLOC_FAILURE:
		result = ISC_R_NOMEMORY;
		break;
	default:
338
#ifdef HAVE_OPENSSL_ECDSA
Tinderbox User's avatar
Tinderbox User committed
339
		if (lib == ERR_R_ECDSA_LIB &&
Evan Hunt's avatar
Evan Hunt committed
340 341 342 343 344
		    reason == ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED) {
			result = ISC_R_NOENTROPY;
			break;
		}
#endif
345 346
		break;
	}
Evan Hunt's avatar
Evan Hunt committed
347 348 349 350 351 352 353 354 355 356

	return (result);
}

isc_result_t
dst__openssl_toresult(isc_result_t fallback) {
	isc_result_t result;

	result = toresult(fallback);

357 358 359 360
	ERR_clear_error();
	return (result);
}

361 362
isc_result_t
dst__openssl_toresult2(const char *funcname, isc_result_t fallback) {
363 364 365 366 367 368 369
	return (dst__openssl_toresult3(DNS_LOGCATEGORY_GENERAL,
				       funcname, fallback));
}

isc_result_t
dst__openssl_toresult3(isc_logcategory_t *category,
		       const char *funcname, isc_result_t fallback) {
Evan Hunt's avatar
Evan Hunt committed
370
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
371
	unsigned long err;
372 373 374 375
	const char *file, *data;
	int line, flags;
	char buf[256];

Evan Hunt's avatar
Evan Hunt committed
376 377
	result = toresult(fallback);

378
	isc_log_write(dns_lctx, category,
379
		      DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
380 381 382 383 384 385
		      "%s failed (%s)", funcname,
		      isc_result_totext(result));

	if (result == ISC_R_NOMEMORY)
		goto done;

386 387
	for (;;) {
		err = ERR_get_error_line_data(&file, &line, &data, &flags);
Mark Andrews's avatar
Mark Andrews committed
388
		if (err == 0U)
389 390
			goto done;
		ERR_error_string_n(err, buf, sizeof(buf));
391
		isc_log_write(dns_lctx, category,
392 393 394 395 396 397 398 399 400 401
			      DNS_LOGMODULE_CRYPTO, ISC_LOG_INFO,
			      "%s:%s:%d:%s", buf, file, line,
			      (flags & ERR_TXT_STRING) ? data : "");
	}

    done:
	ERR_clear_error();
	return (result);
}

402
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
403
ENGINE *
Francis Dupont's avatar
Francis Dupont committed
404
dst__openssl_getengine(const char *engine) {
Francis Dupont's avatar
Francis Dupont committed
405

Francis Dupont's avatar
Francis Dupont committed
406 407
	if (engine == NULL)
		return (NULL);
Francis Dupont's avatar
Francis Dupont committed
408
	if (e == NULL)
Francis Dupont's avatar
Francis Dupont committed
409 410 411 412
		return (NULL);
	if (strcmp(engine, ENGINE_get_id(e)) == 0)
		return (e);
	return (NULL);
Francis Dupont's avatar
Francis Dupont committed
413
}
414
#endif
Francis Dupont's avatar
Francis Dupont committed
415

416 417 418 419
#else /* OPENSSL */

#include <isc/util.h>

Mark Andrews's avatar
Mark Andrews committed
420
EMPTY_TRANSLATION_UNIT
421

422
#endif /* OPENSSL */
423
/*! \file */