openssl_link.c 9.67 KB
Newer Older
1
/*
2
 * Portions Copyright (C) 1999-2012, 2014-2016  Internet Systems Consortium, Inc. ("ISC")
Automatic Updater's avatar
Automatic Updater committed
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Automatic Updater's avatar
Automatic Updater committed
7
 *
8
 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
9
 *
Automatic Updater's avatar
Automatic Updater committed
10
 * Permission to use, copy, modify, and/or distribute this software for any
11 12
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
13
 *
Mark Andrews's avatar
Mark Andrews committed
14 15 16 17 18 19 20
 * 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.
21 22 23 24
 */

/*
 * Principal Author: Brian Wellington
Mark Andrews's avatar
Mark Andrews committed
25
 * $Id$
26
 */
27
#ifdef OPENSSL
28 29 30

#include <config.h>

Brian Wellington's avatar
Brian Wellington committed
31
#include <isc/entropy.h>
32
#include <isc/mem.h>
33 34
#include <isc/mutex.h>
#include <isc/mutexblock.h>
35
#include <isc/string.h>
36
#include <isc/thread.h>
Bob Halley's avatar
Bob Halley committed
37
#include <isc/util.h>
38

39 40
#include <dns/log.h>

Francis Dupont's avatar
Francis Dupont committed
41 42
#include <dst/result.h>

43
#include "dst_internal.h"
44
#include "dst_openssl.h"
45

46
#ifdef USE_ENGINE
47 48
#include <openssl/engine.h>
#endif
49

50
static RAND_METHOD *rm = NULL;
51

52 53
static isc_mutex_t *locks = NULL;
static int nlocks;
54

55
#ifdef USE_ENGINE
Francis Dupont's avatar
Francis Dupont committed
56
static ENGINE *e = NULL;
57 58
#endif

Brian Wellington's avatar
Brian Wellington committed
59 60 61 62 63
static int
entropy_get(unsigned char *buf, int num) {
	isc_result_t result;
	if (num < 0)
		return (-1);
64
	result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
65
	return (result == ISC_R_SUCCESS ? 1 : -1);
66 67
}

68 69 70 71 72
static int
entropy_status(void) {
	return (dst__entropy_status() > 32);
}

73 74 75 76 77 78
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);
79
	return (result == ISC_R_SUCCESS ? 1 : -1);
Brian Wellington's avatar
Brian Wellington committed
80 81
}

82
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Brian Wellington's avatar
Brian Wellington committed
83 84 85 86 87 88 89 90 91
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);
}
92 93 94 95 96 97 98 99 100 101 102 103
#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
104

105
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
106 107
static void
lock_callback(int mode, int type, const char *file, int line) {
108 109
	UNUSED(file);
	UNUSED(line);
110 111 112 113 114 115 116 117 118 119
	if ((mode & CRYPTO_LOCK) != 0)
		LOCK(&locks[type]);
	else
		UNLOCK(&locks[type]);
}

static unsigned long
id_callback(void) {
	return ((unsigned long)isc_thread_self());
}
120
#endif
121

122 123
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
#define FLARG_PASS      , __FILE__, __LINE__
124
#define FLARG
125 126 127 128 129 130 131 132 133 134 135 136
#define FILELINE
#else
#define FLARG           , const char *file, int line
#define FILELINE	, __FILE__, __LINE__
#if ISC_MEM_TRACKLINES
#define FLARG_PASS      , file, line
#else
#define FLARG_PASS
#endif

#endif

137
static void *
138
mem_alloc(size_t size FLARG) {
Francis Dupont's avatar
Francis Dupont committed
139 140 141 142
#ifdef OPENSSL_LEAKS
	void *ptr;

	INSIST(dst__memory_pool != NULL);
143
	ptr = isc__mem_allocate(dst__memory_pool, size FLARG_PASS);
Francis Dupont's avatar
Francis Dupont committed
144 145
	return (ptr);
#else
Mark Andrews's avatar
Mark Andrews committed
146
	INSIST(dst__memory_pool != NULL);
147
	return (isc__mem_allocate(dst__memory_pool, size FLARG_PASS));
Francis Dupont's avatar
Francis Dupont committed
148
#endif
149 150 151
}

static void
152
mem_free(void *ptr FLARG) {
Mark Andrews's avatar
Mark Andrews committed
153
	INSIST(dst__memory_pool != NULL);
154
	if (ptr != NULL)
155
		isc__mem_free(dst__memory_pool, ptr FLARG_PASS);
156 157 158
}

static void *
159
mem_realloc(void *ptr, size_t size FLARG) {
Francis Dupont's avatar
Francis Dupont committed
160 161 162 163
#ifdef OPENSSL_LEAKS
	void *rptr;

	INSIST(dst__memory_pool != NULL);
164
	rptr = isc__mem_reallocate(dst__memory_pool, ptr, size FLARG_PASS);
Francis Dupont's avatar
Francis Dupont committed
165 166
	return (rptr);
#else
Mark Andrews's avatar
Mark Andrews committed
167
	INSIST(dst__memory_pool != NULL);
168
	return (isc__mem_reallocate(dst__memory_pool, ptr, size FLARG_PASS));
Francis Dupont's avatar
Francis Dupont committed
169
#endif
170 171
}

Brian Wellington's avatar
Brian Wellington committed
172
isc_result_t
Francis Dupont's avatar
Francis Dupont committed
173
dst__openssl_init(const char *engine) {
174
	isc_result_t result;
Francis Dupont's avatar
Francis Dupont committed
175 176
#ifdef USE_ENGINE
	ENGINE *re;
Francis Dupont's avatar
Francis Dupont committed
177 178 179
#else

	UNUSED(engine);
Francis Dupont's avatar
Francis Dupont committed
180
#endif
181

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

200 201
	ERR_load_crypto_strings();

202
	rm = mem_alloc(sizeof(RAND_METHOD) FILELINE);
203 204 205 206
	if (rm == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup_mutexinit;
	}
207 208 209 210 211
	rm->seed = NULL;
	rm->bytes = entropy_get;
	rm->cleanup = NULL;
	rm->add = entropy_add;
	rm->pseudorand = entropy_getpseudo;
212
	rm->status = entropy_status;
Francis Dupont's avatar
Francis Dupont committed
213

214
#ifdef USE_ENGINE
215 216 217
#if !defined(CONF_MFLAGS_DEFAULT_SECTION)
	OPENSSL_config(NULL);
#else
218 219 220 221 222 223 224 225 226 227
	/*
	 * 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);
228
#endif
Francis Dupont's avatar
Francis Dupont committed
229 230 231 232 233 234

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

	if (engine != NULL) {
		e = ENGINE_by_id(engine);
Francis Dupont's avatar
Francis Dupont committed
235
		if (e == NULL) {
Francis Dupont's avatar
Francis Dupont committed
236
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
237 238
			goto cleanup_rm;
		}
Francis Dupont's avatar
Francis Dupont committed
239 240 241
		/* This will init the engine. */
		if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
242 243 244
			goto cleanup_rm;
		}
	}
Francis Dupont's avatar
Francis Dupont committed
245

Francis Dupont's avatar
Francis Dupont committed
246 247 248 249 250 251 252 253 254 255 256 257
	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);
258
#else
259
	RAND_set_rand_method(rm);
260
#endif /* USE_ENGINE */
Brian Wellington's avatar
Brian Wellington committed
261
	return (ISC_R_SUCCESS);
262

263
#ifdef USE_ENGINE
264
 cleanup_rm:
Francis Dupont's avatar
Francis Dupont committed
265 266 267
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
268
	mem_free(rm FILELINE);
Francis Dupont's avatar
Francis Dupont committed
269
	rm = NULL;
270 271
#endif
 cleanup_mutexinit:
272
	CRYPTO_set_locking_callback(NULL);
273
	DESTROYMUTEXBLOCK(locks, nlocks);
274
 cleanup_mutexalloc:
275
	mem_free(locks FILELINE);
Francis Dupont's avatar
Francis Dupont committed
276
	locks = NULL;
277
	return (result);
Brian Wellington's avatar
Brian Wellington committed
278 279
}

280
void
281
dst__openssl_destroy(void) {
282 283 284 285 286 287 288
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
	OPENSSL_cleanup();
	if (rm != NULL) {
		mem_free(rm FILELINE);
		rm = NULL;
	}
#else
289 290 291
	/*
	 * Sequence taken from apps_shutdown() in <apps/apps.h>.
	 */
Francis Dupont's avatar
Francis Dupont committed
292 293 294 295
	if (rm != NULL) {
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
		RAND_cleanup();
#endif
296
		mem_free(rm FILELINE);
Francis Dupont's avatar
Francis Dupont committed
297 298
		rm = NULL;
	}
299
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
Francis Dupont's avatar
Francis Dupont committed
300
	CONF_modules_free();
301
#endif
Francis Dupont's avatar
Francis Dupont committed
302
	OBJ_cleanup();
303
	EVP_cleanup();
Francis Dupont's avatar
Francis Dupont committed
304
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
305 306 307
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
308
#if defined(USE_ENGINE) && OPENSSL_VERSION_NUMBER >= 0x00907000L
309 310
	ENGINE_cleanup();
#endif
Francis Dupont's avatar
Francis Dupont committed
311
#endif
312
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
313
	CRYPTO_cleanup_all_ex_data();
314
#endif
315
	ERR_clear_error();
316
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
317
	ERR_remove_state(0);
318
#endif
Francis Dupont's avatar
Francis Dupont committed
319
	ERR_free_strings();
320 321 322 323 324

#ifdef  DNS_CRYPTO_LEAKS
	CRYPTO_mem_leaks_fp(stderr);
#endif

325 326 327
	if (locks != NULL) {
		CRYPTO_set_locking_callback(NULL);
		DESTROYMUTEXBLOCK(locks, nlocks);
328
		mem_free(locks FILELINE);
Francis Dupont's avatar
Francis Dupont committed
329
		locks = NULL;
330
	}
331
#endif
332 333
}

Evan Hunt's avatar
Evan Hunt committed
334 335
static isc_result_t
toresult(isc_result_t fallback) {
336
	isc_result_t result = fallback;
337
	unsigned long err = ERR_get_error();
338 339
#if defined(HAVE_OPENSSL_ECDSA) && \
    defined(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
Evan Hunt's avatar
Evan Hunt committed
340
	int lib = ERR_GET_LIB(err);
Evan Hunt's avatar
Evan Hunt committed
341
#endif
Evan Hunt's avatar
Evan Hunt committed
342
	int reason = ERR_GET_REASON(err);
343

Evan Hunt's avatar
Evan Hunt committed
344 345 346 347 348
	switch (reason) {
	/*
	 * ERR_* errors are globally unique; others
	 * are unique per sublibrary
	 */
349 350 351 352
	case ERR_R_MALLOC_FAILURE:
		result = ISC_R_NOMEMORY;
		break;
	default:
353 354
#if defined(HAVE_OPENSSL_ECDSA) && \
    defined(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
Tinderbox User's avatar
Tinderbox User committed
355
		if (lib == ERR_R_ECDSA_LIB &&
Evan Hunt's avatar
Evan Hunt committed
356 357 358 359 360
		    reason == ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED) {
			result = ISC_R_NOENTROPY;
			break;
		}
#endif
361 362
		break;
	}
Evan Hunt's avatar
Evan Hunt committed
363 364 365 366 367 368 369 370 371 372

	return (result);
}

isc_result_t
dst__openssl_toresult(isc_result_t fallback) {
	isc_result_t result;

	result = toresult(fallback);

373 374 375 376
	ERR_clear_error();
	return (result);
}

377 378
isc_result_t
dst__openssl_toresult2(const char *funcname, isc_result_t fallback) {
379 380 381 382 383 384 385
	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
386
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
387
	unsigned long err;
388 389 390 391
	const char *file, *data;
	int line, flags;
	char buf[256];

Evan Hunt's avatar
Evan Hunt committed
392 393
	result = toresult(fallback);

394
	isc_log_write(dns_lctx, category,
395
		      DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
396 397 398 399 400 401
		      "%s failed (%s)", funcname,
		      isc_result_totext(result));

	if (result == ISC_R_NOMEMORY)
		goto done;

402 403
	for (;;) {
		err = ERR_get_error_line_data(&file, &line, &data, &flags);
Mark Andrews's avatar
Mark Andrews committed
404
		if (err == 0U)
405 406
			goto done;
		ERR_error_string_n(err, buf, sizeof(buf));
407
		isc_log_write(dns_lctx, category,
408 409 410 411 412 413 414 415 416 417
			      DNS_LOGMODULE_CRYPTO, ISC_LOG_INFO,
			      "%s:%s:%d:%s", buf, file, line,
			      (flags & ERR_TXT_STRING) ? data : "");
	}

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

418
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
419
ENGINE *
Francis Dupont's avatar
Francis Dupont committed
420
dst__openssl_getengine(const char *engine) {
Francis Dupont's avatar
Francis Dupont committed
421

Francis Dupont's avatar
Francis Dupont committed
422 423
	if (engine == NULL)
		return (NULL);
Francis Dupont's avatar
Francis Dupont committed
424
	if (e == NULL)
Francis Dupont's avatar
Francis Dupont committed
425 426 427 428
		return (NULL);
	if (strcmp(engine, ENGINE_get_id(e)) == 0)
		return (e);
	return (NULL);
Francis Dupont's avatar
Francis Dupont committed
429
}
430
#endif
Francis Dupont's avatar
Francis Dupont committed
431

432 433 434 435
#else /* OPENSSL */

#include <isc/util.h>

Mark Andrews's avatar
Mark Andrews committed
436
EMPTY_TRANSLATION_UNIT
437

438
#endif /* OPENSSL */
439
/*! \file */