openssl_link.c 9.93 KB
Newer Older
1
/*
2
 * Portions Copyright (C) 1999-2012, 2014-2017  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
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
125

126
#define FLARG
127
#define FILELINE
128 129
#if ISC_MEM_TRACKLINES
#define FLARG_PASS      , __FILE__, __LINE__
130
#else
131 132 133 134 135
#define FLARG_PASS
#endif

#else

136 137 138 139 140 141 142 143 144 145
#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

146
static void *
147
mem_alloc(size_t size FLARG) {
Francis Dupont's avatar
Francis Dupont committed
148 149 150 151
#ifdef OPENSSL_LEAKS
	void *ptr;

	INSIST(dst__memory_pool != NULL);
152
	ptr = isc__mem_allocate(dst__memory_pool, size FLARG_PASS);
Francis Dupont's avatar
Francis Dupont committed
153 154
	return (ptr);
#else
Mark Andrews's avatar
Mark Andrews committed
155
	INSIST(dst__memory_pool != NULL);
156
	return (isc__mem_allocate(dst__memory_pool, size FLARG_PASS));
Francis Dupont's avatar
Francis Dupont committed
157
#endif
158 159 160
}

static void
161
mem_free(void *ptr FLARG) {
Mark Andrews's avatar
Mark Andrews committed
162
	INSIST(dst__memory_pool != NULL);
163
	if (ptr != NULL)
164
		isc__mem_free(dst__memory_pool, ptr FLARG_PASS);
165 166 167
}

static void *
168
mem_realloc(void *ptr, size_t size FLARG) {
Francis Dupont's avatar
Francis Dupont committed
169 170 171 172
#ifdef OPENSSL_LEAKS
	void *rptr;

	INSIST(dst__memory_pool != NULL);
173
	rptr = isc__mem_reallocate(dst__memory_pool, ptr, size FLARG_PASS);
Francis Dupont's avatar
Francis Dupont committed
174 175
	return (rptr);
#else
Mark Andrews's avatar
Mark Andrews committed
176
	INSIST(dst__memory_pool != NULL);
177
	return (isc__mem_reallocate(dst__memory_pool, ptr, size FLARG_PASS));
Francis Dupont's avatar
Francis Dupont committed
178
#endif
179 180
}

Brian Wellington's avatar
Brian Wellington committed
181
isc_result_t
Francis Dupont's avatar
Francis Dupont committed
182
dst__openssl_init(const char *engine) {
183
	isc_result_t result;
Francis Dupont's avatar
Francis Dupont committed
184 185
#ifdef USE_ENGINE
	ENGINE *re;
Francis Dupont's avatar
Francis Dupont committed
186 187 188
#else

	UNUSED(engine);
Francis Dupont's avatar
Francis Dupont committed
189
#endif
190

191 192 193 194 195
#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
196
	CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
197
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
198
	nlocks = CRYPTO_num_locks();
199
	locks = mem_alloc(sizeof(isc_mutex_t) * nlocks FILELINE);
200 201 202
	if (locks == NULL)
		return (ISC_R_NOMEMORY);
	result = isc_mutexblock_init(locks, nlocks);
203 204
	if (result != ISC_R_SUCCESS)
		goto cleanup_mutexalloc;
205
	CRYPTO_set_locking_callback(lock_callback);
206
	CRYPTO_set_id_callback(id_callback);
207

208
	ERR_load_crypto_strings();
209
#endif
210

211
	rm = mem_alloc(sizeof(RAND_METHOD) FILELINE);
212 213 214 215
	if (rm == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup_mutexinit;
	}
216 217 218 219 220
	rm->seed = NULL;
	rm->bytes = entropy_get;
	rm->cleanup = NULL;
	rm->add = entropy_add;
	rm->pseudorand = entropy_getpseudo;
221
	rm->status = entropy_status;
Francis Dupont's avatar
Francis Dupont committed
222

223
#ifdef USE_ENGINE
224 225 226
#if !defined(CONF_MFLAGS_DEFAULT_SECTION)
	OPENSSL_config(NULL);
#else
227 228 229 230 231 232 233 234 235 236
	/*
	 * 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);
237
#endif
Francis Dupont's avatar
Francis Dupont committed
238 239 240 241 242 243

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

	if (engine != NULL) {
		e = ENGINE_by_id(engine);
Francis Dupont's avatar
Francis Dupont committed
244
		if (e == NULL) {
Francis Dupont's avatar
Francis Dupont committed
245
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
246 247
			goto cleanup_rm;
		}
Francis Dupont's avatar
Francis Dupont committed
248 249 250
		/* This will init the engine. */
		if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
			result = DST_R_NOENGINE;
Francis Dupont's avatar
Francis Dupont committed
251 252 253
			goto cleanup_rm;
		}
	}
Francis Dupont's avatar
Francis Dupont committed
254

Francis Dupont's avatar
Francis Dupont committed
255 256 257 258 259 260 261 262 263 264 265 266
	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);
267
#else
268
	RAND_set_rand_method(rm);
269
#endif /* USE_ENGINE */
Brian Wellington's avatar
Brian Wellington committed
270
	return (ISC_R_SUCCESS);
271

272
#ifdef USE_ENGINE
273
 cleanup_rm:
Francis Dupont's avatar
Francis Dupont committed
274 275 276
	if (e != NULL)
		ENGINE_free(e);
	e = NULL;
277
	mem_free(rm FILELINE);
Francis Dupont's avatar
Francis Dupont committed
278
	rm = NULL;
279 280
#endif
 cleanup_mutexinit:
281
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
282
	CRYPTO_set_locking_callback(NULL);
283
	DESTROYMUTEXBLOCK(locks, nlocks);
284
 cleanup_mutexalloc:
285
	mem_free(locks FILELINE);
Francis Dupont's avatar
Francis Dupont committed
286
	locks = NULL;
287
#endif
288
	return (result);
Brian Wellington's avatar
Brian Wellington committed
289 290
}

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

#ifdef  DNS_CRYPTO_LEAKS
	CRYPTO_mem_leaks_fp(stderr);
#endif

336 337 338
	if (locks != NULL) {
		CRYPTO_set_locking_callback(NULL);
		DESTROYMUTEXBLOCK(locks, nlocks);
339
		mem_free(locks FILELINE);
Francis Dupont's avatar
Francis Dupont committed
340
		locks = NULL;
341
	}
342
#endif
343 344
}

Evan Hunt's avatar
Evan Hunt committed
345 346
static isc_result_t
toresult(isc_result_t fallback) {
347
	isc_result_t result = fallback;
348
	unsigned long err = ERR_get_error();
349 350
#if defined(HAVE_OPENSSL_ECDSA) && \
    defined(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
Evan Hunt's avatar
Evan Hunt committed
351
	int lib = ERR_GET_LIB(err);
Evan Hunt's avatar
Evan Hunt committed
352
#endif
Evan Hunt's avatar
Evan Hunt committed
353
	int reason = ERR_GET_REASON(err);
354

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

	return (result);
}

isc_result_t
dst__openssl_toresult(isc_result_t fallback) {
	isc_result_t result;

	result = toresult(fallback);

384 385 386 387
	ERR_clear_error();
	return (result);
}

388 389
isc_result_t
dst__openssl_toresult2(const char *funcname, isc_result_t fallback) {
390 391 392 393 394 395 396
	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
397
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
398
	unsigned long err;
399 400 401 402
	const char *file, *data;
	int line, flags;
	char buf[256];

Evan Hunt's avatar
Evan Hunt committed
403 404
	result = toresult(fallback);

405
	isc_log_write(dns_lctx, category,
406
		      DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING,
Evan Hunt's avatar
Evan Hunt committed
407 408 409 410 411 412
		      "%s failed (%s)", funcname,
		      isc_result_totext(result));

	if (result == ISC_R_NOMEMORY)
		goto done;

413 414
	for (;;) {
		err = ERR_get_error_line_data(&file, &line, &data, &flags);
Mark Andrews's avatar
Mark Andrews committed
415
		if (err == 0U)
416 417
			goto done;
		ERR_error_string_n(err, buf, sizeof(buf));
418
		isc_log_write(dns_lctx, category,
419 420 421 422 423 424 425 426 427 428
			      DNS_LOGMODULE_CRYPTO, ISC_LOG_INFO,
			      "%s:%s:%d:%s", buf, file, line,
			      (flags & ERR_TXT_STRING) ? data : "");
	}

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

429
#if defined(USE_ENGINE)
Francis Dupont's avatar
Francis Dupont committed
430
ENGINE *
Francis Dupont's avatar
Francis Dupont committed
431
dst__openssl_getengine(const char *engine) {
Francis Dupont's avatar
Francis Dupont committed
432

Francis Dupont's avatar
Francis Dupont committed
433 434
	if (engine == NULL)
		return (NULL);
Francis Dupont's avatar
Francis Dupont committed
435
	if (e == NULL)
Francis Dupont's avatar
Francis Dupont committed
436 437 438 439
		return (NULL);
	if (strcmp(engine, ENGINE_get_id(e)) == 0)
		return (e);
	return (NULL);
Francis Dupont's avatar
Francis Dupont committed
440
}
441
#endif
Francis Dupont's avatar
Francis Dupont committed
442

443 444 445 446
#else /* OPENSSL */

#include <isc/util.h>

Mark Andrews's avatar
Mark Andrews committed
447
EMPTY_TRANSLATION_UNIT
448

449
#endif /* OPENSSL */
450
/*! \file */