openssl_link.c 9.88 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
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
53 54
static isc_mutex_t *locks = NULL;
static int nlocks;
55
#endif
56

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

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

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

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

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

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

static unsigned long
id_callback(void) {
	return ((unsigned long)isc_thread_self());
}
122
#endif
123

124 125
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
#define FLARG_PASS      , __FILE__, __LINE__
126
#define FLARG
127 128 129 130 131 132 133 134 135 136 137 138
#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

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

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

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

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

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

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

	UNUSED(engine);
Francis Dupont's avatar
Francis Dupont committed
182
#endif
183

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

202 203
	ERR_load_crypto_strings();

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

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

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

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

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

265
#ifdef USE_ENGINE
266
 cleanup_rm:
Francis Dupont's avatar
Francis Dupont committed
267 268 269
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
270
	mem_free(rm FILELINE);
Francis Dupont's avatar
Francis Dupont committed
271
	rm = NULL;
272 273
#endif
 cleanup_mutexinit:
274
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
275
	CRYPTO_set_locking_callback(NULL);
276
	DESTROYMUTEXBLOCK(locks, nlocks);
277
 cleanup_mutexalloc:
278
	mem_free(locks FILELINE);
Francis Dupont's avatar
Francis Dupont committed
279
	locks = NULL;
280
#endif
281
	return (result);
Brian Wellington's avatar
Brian Wellington committed
282 283
}

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

#ifdef  DNS_CRYPTO_LEAKS
	CRYPTO_mem_leaks_fp(stderr);
#endif

329 330 331
	if (locks != NULL) {
		CRYPTO_set_locking_callback(NULL);
		DESTROYMUTEXBLOCK(locks, nlocks);
332
		mem_free(locks FILELINE);
Francis Dupont's avatar
Francis Dupont committed
333
		locks = NULL;
334
	}
335
#endif
336 337
}

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

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

	return (result);
}

isc_result_t
dst__openssl_toresult(isc_result_t fallback) {
	isc_result_t result;

	result = toresult(fallback);

377 378 379 380
	ERR_clear_error();
	return (result);
}

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

Evan Hunt's avatar
Evan Hunt committed
396 397
	result = toresult(fallback);

398
	isc_log_write(dns_lctx, category,
399
		      DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
400 401 402 403 404 405
		      "%s failed (%s)", funcname,
		      isc_result_totext(result));

	if (result == ISC_R_NOMEMORY)
		goto done;

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

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

422
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
423
ENGINE *
Francis Dupont's avatar
Francis Dupont committed
424
dst__openssl_getengine(const char *engine) {
Francis Dupont's avatar
Francis Dupont committed
425

Francis Dupont's avatar
Francis Dupont committed
426 427
	if (engine == NULL)
		return (NULL);
Francis Dupont's avatar
Francis Dupont committed
428
	if (e == NULL)
Francis Dupont's avatar
Francis Dupont committed
429 430 431 432
		return (NULL);
	if (strcmp(engine, ENGINE_get_id(e)) == 0)
		return (e);
	return (NULL);
Francis Dupont's avatar
Francis Dupont committed
433
}
434
#endif
Francis Dupont's avatar
Francis Dupont committed
435

436 437 438 439
#else /* OPENSSL */

#include <isc/util.h>

Mark Andrews's avatar
Mark Andrews committed
440
EMPTY_TRANSLATION_UNIT
441

442
#endif /* OPENSSL */
443
/*! \file */