1999-07-12 20:08:42 +00:00
|
|
|
/*
|
2018-03-15 18:32:45 -07:00
|
|
|
* Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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/.
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
2018-02-23 09:53:12 +01:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*
|
|
|
|
* Portions Copyright (C) Network Associates, Inc.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2007-08-28 07:20:43 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
1999-07-12 20:08:42 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* 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.
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
2000-12-04 23:06:37 +00:00
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/mutexblock.h>
|
2017-09-12 19:05:46 -07:00
|
|
|
#include <isc/platform.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-12-04 23:06:37 +00:00
|
|
|
#include <isc/thread.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2012-07-23 15:08:21 +10:00
|
|
|
#include <dns/log.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#include "dst_internal.h"
|
2002-03-19 04:30:57 +00:00
|
|
|
#include "dst_openssl.h"
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <dst/result.h>
|
|
|
|
|
2018-10-25 11:27:28 -07:00
|
|
|
static isc_mem_t *dst__mctx = NULL;
|
2018-10-11 15:13:27 +00:00
|
|
|
|
2018-04-04 21:50:16 +02:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2001-02-14 20:57:15 +00:00
|
|
|
#include <openssl/engine.h>
|
|
|
|
#endif
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2016-10-31 16:33:22 +11:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
|
2001-02-14 20:26:48 +00:00
|
|
|
static isc_mutex_t *locks = NULL;
|
2020-02-12 13:59:18 +01:00
|
|
|
static int nlocks;
|
2016-10-31 16:33:22 +11:00
|
|
|
#endif
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2018-04-04 21:50:16 +02:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2009-10-05 17:30:49 +00:00
|
|
|
static ENGINE *e = NULL;
|
2001-02-14 20:57:15 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-14 14:32:02 +02:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
enable_fips_mode(void)
|
|
|
|
{
|
2018-10-14 14:32:02 +02:00
|
|
|
#ifdef HAVE_FIPS_MODE
|
|
|
|
if (FIPS_mode() != 0) {
|
|
|
|
/*
|
|
|
|
* FIPS mode is already enabled.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FIPS_mode_set(1) == 0) {
|
|
|
|
dst__openssl_toresult2("FIPS_mode_set", DST_R_OPENSSLFAILURE);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_FIPS_MODE */
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:03:50 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
|
2000-12-04 23:06:37 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
lock_callback(int mode, int type, const char *file, int line)
|
|
|
|
{
|
2000-12-04 23:39:05 +00:00
|
|
|
UNUSED(file);
|
|
|
|
UNUSED(line);
|
2000-12-04 23:06:37 +00:00
|
|
|
if ((mode & CRYPTO_LOCK) != 0)
|
|
|
|
LOCK(&locks[type]);
|
|
|
|
else
|
|
|
|
UNLOCK(&locks[type]);
|
|
|
|
}
|
2017-08-10 10:16:26 +10:00
|
|
|
#endif
|
2000-12-04 23:06:37 +00:00
|
|
|
|
2018-05-03 14:03:50 +02:00
|
|
|
#if defined(LIBRESSL_VERSION_NUMBER)
|
2000-12-04 23:06:37 +00:00
|
|
|
static unsigned long
|
2020-02-12 13:59:18 +01:00
|
|
|
id_callback(void)
|
|
|
|
{
|
2000-12-04 23:06:37 +00:00
|
|
|
return ((unsigned long)isc_thread_self());
|
|
|
|
}
|
2015-05-28 14:41:21 +10:00
|
|
|
#endif
|
2000-12-04 23:06:37 +00:00
|
|
|
|
2018-05-03 14:03:50 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
2017-08-10 10:16:26 +10:00
|
|
|
static void
|
|
|
|
_set_thread_id(CRYPTO_THREADID *id)
|
|
|
|
{
|
2017-08-14 13:51:20 +10:00
|
|
|
CRYPTO_THREADID_set_numeric(id, (unsigned long)isc_thread_self());
|
2017-08-10 10:16:26 +10:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-09 22:32:20 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dst__openssl_init(isc_mem_t *mctx, const char *engine)
|
|
|
|
{
|
2000-12-04 23:06:37 +00:00
|
|
|
isc_result_t result;
|
2009-10-05 17:30:49 +00:00
|
|
|
|
2018-10-25 11:27:28 -07:00
|
|
|
REQUIRE(dst__mctx == NULL);
|
|
|
|
isc_mem_attach(mctx, &dst__mctx);
|
2018-10-11 15:13:27 +00:00
|
|
|
|
2018-04-04 21:50:16 +02:00
|
|
|
#if defined(OPENSSL_NO_ENGINE)
|
2009-10-05 17:30:49 +00:00
|
|
|
UNUSED(engine);
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-12-04 23:06:37 +00:00
|
|
|
|
2018-10-14 14:32:02 +02:00
|
|
|
enable_fips_mode();
|
|
|
|
|
2016-10-31 16:33:22 +11:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
|
2001-02-14 20:26:48 +00:00
|
|
|
nlocks = CRYPTO_num_locks();
|
2018-10-25 11:27:28 -07:00
|
|
|
locks = isc_mem_allocate(dst__mctx, sizeof(isc_mutex_t) * nlocks);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutexblock_init(locks, nlocks);
|
2016-10-31 10:04:37 +11:00
|
|
|
CRYPTO_set_locking_callback(lock_callback);
|
2020-02-12 13:59:18 +01:00
|
|
|
#if defined(LIBRESSL_VERSION_NUMBER)
|
2000-12-04 23:06:37 +00:00
|
|
|
CRYPTO_set_id_callback(id_callback);
|
2020-02-12 13:59:18 +01:00
|
|
|
#elif OPENSSL_VERSION_NUMBER < 0x10100000L
|
2018-05-03 14:03:50 +02:00
|
|
|
CRYPTO_THREADID_set_callback(_set_thread_id);
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif
|
2012-07-23 15:08:21 +10:00
|
|
|
ERR_load_crypto_strings();
|
2017-04-21 18:56:00 -07:00
|
|
|
#endif
|
2012-07-23 15:08:21 +10:00
|
|
|
|
2018-04-04 21:50:16 +02:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2016-04-06 04:31:06 +10:00
|
|
|
#if !defined(CONF_MFLAGS_DEFAULT_SECTION)
|
|
|
|
OPENSSL_config(NULL);
|
|
|
|
#else
|
2015-12-24 10:31:07 +11:00
|
|
|
/*
|
|
|
|
* 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 |
|
2020-02-12 13:59:18 +01:00
|
|
|
CONF_MFLAGS_IGNORE_MISSING_FILE);
|
2016-04-06 04:31:06 +10:00
|
|
|
#endif
|
2009-10-05 17:30:49 +00:00
|
|
|
|
|
|
|
if (engine != NULL && *engine == '\0')
|
|
|
|
engine = NULL;
|
|
|
|
|
|
|
|
if (engine != NULL) {
|
|
|
|
e = ENGINE_by_id(engine);
|
2008-03-31 14:42:51 +00:00
|
|
|
if (e == NULL) {
|
2009-10-05 17:30:49 +00:00
|
|
|
result = DST_R_NOENGINE;
|
2008-03-31 14:42:51 +00:00
|
|
|
goto cleanup_rm;
|
|
|
|
}
|
2009-10-05 17:30:49 +00:00
|
|
|
/* This will init the engine. */
|
|
|
|
if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
|
|
|
|
result = DST_R_NOENGINE;
|
2008-03-31 14:42:51 +00:00
|
|
|
goto cleanup_rm;
|
|
|
|
}
|
|
|
|
}
|
2009-10-05 17:30:49 +00:00
|
|
|
|
2018-04-04 21:50:16 +02:00
|
|
|
#endif /* !defined(OPENSSL_NO_ENGINE) */
|
2017-09-12 19:05:46 -07:00
|
|
|
|
|
|
|
/* Protect ourselves against unseeded PRNG */
|
|
|
|
if (RAND_status() != 1) {
|
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
2017-09-12 22:49:35 -07:00
|
|
|
"OpenSSL pseudorandom number generator "
|
|
|
|
"cannot be initialized (see the `PRNG not "
|
2017-09-12 19:05:46 -07:00
|
|
|
"seeded' message in the OpenSSL FAQ)");
|
|
|
|
}
|
|
|
|
|
2000-06-09 22:32:20 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2001-02-14 20:57:15 +00:00
|
|
|
|
2018-04-04 21:50:16 +02:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup_rm:
|
2009-10-05 17:30:49 +00:00
|
|
|
if (e != NULL)
|
|
|
|
ENGINE_free(e);
|
|
|
|
e = NULL;
|
2017-09-12 19:05:46 -07:00
|
|
|
#endif
|
2016-10-31 16:33:22 +11:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
|
2007-08-27 03:04:17 +00:00
|
|
|
CRYPTO_set_locking_callback(NULL);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutexblock_destroy(locks, nlocks);
|
2018-10-25 11:27:28 -07:00
|
|
|
isc_mem_free(dst__mctx, locks);
|
2009-10-05 17:30:49 +00:00
|
|
|
locks = NULL;
|
2016-10-31 16:33:22 +11:00
|
|
|
#endif
|
2001-02-14 20:57:15 +00:00
|
|
|
return (result);
|
2000-06-09 22:32:20 +00:00
|
|
|
}
|
|
|
|
|
2000-06-09 23:31:55 +00:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dst__openssl_destroy(void)
|
|
|
|
{
|
2018-05-03 14:03:50 +02:00
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
|
2005-06-17 02:22:45 +00:00
|
|
|
/*
|
|
|
|
* Sequence taken from apps_shutdown() in <apps/apps.h>.
|
|
|
|
*/
|
2009-10-05 17:30:49 +00:00
|
|
|
CONF_modules_free();
|
|
|
|
OBJ_cleanup();
|
2005-06-17 02:22:45 +00:00
|
|
|
EVP_cleanup();
|
2018-04-04 21:50:16 +02:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2009-10-05 17:30:49 +00:00
|
|
|
if (e != NULL)
|
|
|
|
ENGINE_free(e);
|
|
|
|
e = NULL;
|
2005-06-17 02:22:45 +00:00
|
|
|
ENGINE_cleanup();
|
|
|
|
#endif
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
ERR_clear_error();
|
2018-05-03 14:03:50 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
2017-08-10 10:16:26 +10:00
|
|
|
ERR_remove_thread_state(NULL);
|
2018-05-03 14:03:50 +02:00
|
|
|
#elif defined(LIBRESSL_VERSION_NUMBER)
|
2005-06-17 02:22:45 +00:00
|
|
|
ERR_remove_state(0);
|
2015-05-28 14:41:21 +10:00
|
|
|
#endif
|
2009-10-05 17:30:49 +00:00
|
|
|
ERR_free_strings();
|
2005-06-17 02:22:45 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#ifdef DNS_CRYPTO_LEAKS
|
2005-06-17 02:22:45 +00:00
|
|
|
CRYPTO_mem_leaks_fp(stderr);
|
|
|
|
#endif
|
|
|
|
|
2007-08-27 03:04:17 +00:00
|
|
|
if (locks != NULL) {
|
|
|
|
CRYPTO_set_locking_callback(NULL);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutexblock_destroy(locks, nlocks);
|
2018-10-25 11:27:28 -07:00
|
|
|
isc_mem_free(dst__mctx, locks);
|
2009-10-05 17:30:49 +00:00
|
|
|
locks = NULL;
|
2007-08-27 03:04:17 +00:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
#endif
|
2018-10-25 11:27:28 -07:00
|
|
|
isc_mem_detach(&dst__mctx);
|
2000-06-09 23:31:55 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 13:46:47 -07:00
|
|
|
static isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
toresult(isc_result_t fallback)
|
|
|
|
{
|
|
|
|
isc_result_t result = fallback;
|
2018-08-14 12:49:19 +02:00
|
|
|
unsigned long err = ERR_peek_error();
|
2018-06-12 11:26:04 +02:00
|
|
|
#if defined(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
|
2012-09-28 13:46:47 -07:00
|
|
|
int lib = ERR_GET_LIB(err);
|
2012-09-28 18:47:05 -07:00
|
|
|
#endif
|
2012-09-28 13:46:47 -07:00
|
|
|
int reason = ERR_GET_REASON(err);
|
2002-03-19 04:30:57 +00:00
|
|
|
|
2012-09-28 13:46:47 -07:00
|
|
|
switch (reason) {
|
|
|
|
/*
|
|
|
|
* ERR_* errors are globally unique; others
|
|
|
|
* are unique per sublibrary
|
|
|
|
*/
|
2002-03-19 04:30:57 +00:00
|
|
|
case ERR_R_MALLOC_FAILURE:
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-12 11:26:04 +02:00
|
|
|
#if defined(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
|
2012-09-28 23:46:10 +00:00
|
|
|
if (lib == ERR_R_ECDSA_LIB &&
|
2012-09-28 13:46:47 -07:00
|
|
|
reason == ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED) {
|
|
|
|
result = ISC_R_NOENTROPY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2002-03-19 04:30:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-09-28 13:46:47 -07:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dst__openssl_toresult(isc_result_t fallback)
|
|
|
|
{
|
2012-09-28 13:46:47 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
result = toresult(fallback);
|
|
|
|
|
2002-03-19 04:30:57 +00:00
|
|
|
ERR_clear_error();
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2012-07-23 15:08:21 +10:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dst__openssl_toresult2(const char *funcname, isc_result_t fallback)
|
|
|
|
{
|
|
|
|
return (dst__openssl_toresult3(DNS_LOGCATEGORY_GENERAL, funcname,
|
|
|
|
fallback));
|
2012-10-24 12:58:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dst__openssl_toresult3(isc_logcategory_t *category, const char *funcname,
|
|
|
|
isc_result_t fallback)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
2012-10-12 21:57:22 +11:00
|
|
|
unsigned long err;
|
2020-02-12 13:59:18 +01:00
|
|
|
const char * file, *data;
|
|
|
|
int line, flags;
|
|
|
|
char buf[256];
|
2012-07-23 15:08:21 +10:00
|
|
|
|
2012-09-28 13:46:47 -07:00
|
|
|
result = toresult(fallback);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_log_write(dns_lctx, category, DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING,
|
|
|
|
"%s failed (%s)", funcname, isc_result_totext(result));
|
2012-09-28 13:46:47 -07:00
|
|
|
|
|
|
|
if (result == ISC_R_NOMEMORY)
|
|
|
|
goto done;
|
|
|
|
|
2012-07-23 15:08:21 +10:00
|
|
|
for (;;) {
|
|
|
|
err = ERR_get_error_line_data(&file, &line, &data, &flags);
|
2012-08-14 14:29:47 +10:00
|
|
|
if (err == 0U)
|
2012-07-23 15:08:21 +10:00
|
|
|
goto done;
|
|
|
|
ERR_error_string_n(err, buf, sizeof(buf));
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_log_write(dns_lctx, category, DNS_LOGMODULE_CRYPTO,
|
|
|
|
ISC_LOG_INFO, "%s:%s:%d:%s", buf, file, line,
|
2018-10-11 11:57:57 +02:00
|
|
|
((flags & ERR_TXT_STRING) != 0) ? data : "");
|
2012-07-23 15:08:21 +10:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
done:
|
2012-07-23 15:08:21 +10:00
|
|
|
ERR_clear_error();
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:50:16 +02:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2008-03-31 14:42:51 +00:00
|
|
|
ENGINE *
|
2020-02-12 13:59:18 +01:00
|
|
|
dst__openssl_getengine(const char *engine)
|
|
|
|
{
|
2009-10-05 17:30:49 +00:00
|
|
|
if (engine == NULL)
|
|
|
|
return (NULL);
|
2008-03-31 14:42:51 +00:00
|
|
|
if (e == NULL)
|
2009-10-05 17:30:49 +00:00
|
|
|
return (NULL);
|
|
|
|
if (strcmp(engine, ENGINE_get_id(e)) == 0)
|
|
|
|
return (e);
|
|
|
|
return (NULL);
|
2008-03-31 14:42:51 +00:00
|
|
|
}
|
2011-03-11 01:11:54 +00:00
|
|
|
#endif
|
2008-03-31 14:42:51 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|