openssl_link.c revision 832f5803f69f4548ba3777f8b5741768bbbf5f75
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein/*
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews * Portions Copyright (C) 1999-2012, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
75c0816e8295e180f4bc7f10db3d0d880383bc1cMark Andrews *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * This Source Code Form is subject to the terms of the Mozilla Public
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * License, v. 2.0. If a copy of the MPL was not distributed with this
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * file, You can obtain one at http://mozilla.org/MPL/2.0/.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Permission to use, copy, modify, and/or distribute this software for any
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * purpose with or without fee is hereby granted, provided that the above
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * copyright notice and this permission notice appear in all copies.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
e21a2904f02a03fa06b6db04d348f65fe9c67b2bMark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein/*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Principal Author: Brian Wellington
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * $Id$
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#ifdef OPENSSL
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <config.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <isc/entropy.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <isc/mem.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <isc/mutex.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <isc/mutexblock.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <isc/string.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <isc/thread.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <isc/util.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <dns/log.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <dst/result.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
92554adb458c7b336e5390c5c9fbf2fcc1276524Mark Andrews#include "dst_internal.h"
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#include "dst_openssl.h"
75c0816e8295e180f4bc7f10db3d0d880383bc1cMark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#ifdef USE_ENGINE
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <openssl/engine.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic RAND_METHOD *rm = NULL;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic isc_mutex_t *locks = NULL;
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsstatic int nlocks;
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#endif
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#ifdef USE_ENGINE
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic ENGINE *e = NULL;
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#endif
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsstatic int
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsentropy_get(unsigned char *buf, int num) {
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews isc_result_t result;
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews if (num < 0)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (-1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews return (result == ISC_R_SUCCESS ? 1 : -1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsstatic int
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsentropy_status(void) {
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews return (dst__entropy_status() > 32);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic int
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinentropy_getpseudo(unsigned char *buf, int num) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein isc_result_t result;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (num < 0)
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews return (-1);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews result = dst__entropy_getdata(buf, (unsigned int) num, ISC_TRUE);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (result == ISC_R_SUCCESS ? 1 : -1);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews}
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsstatic void
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsentropy_add(const void *buf, int num, double entropy) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Do nothing. The only call to this provides no useful data anyway.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein UNUSED(buf);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein UNUSED(num);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein UNUSED(entropy);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#else
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsstatic int
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsentropy_add(const void *buf, int num, double entropy) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Do nothing. The only call to this provides no useful data anyway.
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews */
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews UNUSED(buf);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews UNUSED(num);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews UNUSED(entropy);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews return (1);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic void
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinlock_callback(int mode, int type, const char *file, int line) {
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews UNUSED(file);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews UNUSED(line);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews if ((mode & CRYPTO_LOCK) != 0)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein LOCK(&locks[type]);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews else
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein UNLOCK(&locks[type]);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews}
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic unsigned long
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinid_callback(void) {
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews return ((unsigned long)isc_thread_self());
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define FLARG
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define FILELINE
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#if ISC_MEM_TRACKLINES
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define FLARG_PASS , __FILE__, __LINE__
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#else
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define FLARG_PASS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#else
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define FLARG , const char *file, int line
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#define FILELINE , __FILE__, __LINE__
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#if ISC_MEM_TRACKLINES
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define FLARG_PASS , file, line
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#else
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#define FLARG_PASS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#endif
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsstatic void *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinmem_alloc(size_t size FLARG) {
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#ifdef OPENSSL_LEAKS
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews void *ptr;
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews INSIST(dst__memory_pool != NULL);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein ptr = isc__mem_allocate(dst__memory_pool, size FLARG_PASS);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews return (ptr);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#else
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein INSIST(dst__memory_pool != NULL);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (isc__mem_allocate(dst__memory_pool, size FLARG_PASS));
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsstatic void
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinmem_free(void *ptr FLARG) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein INSIST(dst__memory_pool != NULL);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews if (ptr != NULL)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein isc__mem_free(dst__memory_pool, ptr FLARG_PASS);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic void *
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsmem_realloc(void *ptr, size_t size FLARG) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#ifdef OPENSSL_LEAKS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein void *rptr;
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein INSIST(dst__memory_pool != NULL);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews rptr = isc__mem_reallocate(dst__memory_pool, ptr, size FLARG_PASS);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews return (rptr);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#else
7208386cd37a2092c70eddf80cf29519b16c4c80Mark Andrews INSIST(dst__memory_pool != NULL);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (isc__mem_reallocate(dst__memory_pool, ptr, size FLARG_PASS));
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinisc_result_t
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrewsdst__openssl_init(const char *engine) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein isc_result_t result;
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#ifdef USE_ENGINE
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews ENGINE *re;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#else
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein UNUSED(engine);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews#ifdef DNS_CRYPTO_LEAKS
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews CRYPTO_malloc_debug_init();
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews nlocks = CRYPTO_num_locks();
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews locks = mem_alloc(sizeof(isc_mutex_t) * nlocks FILELINE);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews if (locks == NULL)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (ISC_R_NOMEMORY);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein result = isc_mutexblock_init(locks, nlocks);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (result != ISC_R_SUCCESS)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein goto cleanup_mutexalloc;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein CRYPTO_set_locking_callback(lock_callback);
297be3708069ef31814d6d75c0d71a50a78feb03Mark Andrews CRYPTO_set_id_callback(id_callback);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews ERR_load_crypto_strings();
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews rm = mem_alloc(sizeof(RAND_METHOD) FILELINE);
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews if (rm == NULL) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews result = ISC_R_NOMEMORY;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews goto cleanup_mutexinit;
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews }
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews rm->seed = NULL;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews rm->bytes = entropy_get;
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews rm->cleanup = NULL;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews rm->add = entropy_add;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews rm->pseudorand = entropy_getpseudo;
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews rm->status = entropy_status;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#ifdef USE_ENGINE
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews#if !defined(CONF_MFLAGS_DEFAULT_SECTION)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews OPENSSL_config(NULL);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#else
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews /*
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * OPENSSL_config() can only be called a single time as of
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * 1.0.2e so do the steps individually.
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews OPENSSL_load_builtin_modules();
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews ENGINE_load_builtin_engines();
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews ERR_clear_error();
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews CONF_modules_load_file(NULL, NULL,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews CONF_MFLAGS_DEFAULT_SECTION |
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews CONF_MFLAGS_IGNORE_MISSING_FILE);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#endif
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews if (engine != NULL && *engine == '\0')
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews engine = NULL;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (engine != NULL) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein e = ENGINE_by_id(engine);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (e == NULL) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein result = DST_R_NOENGINE;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein goto cleanup_rm;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /* This will init the engine. */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein result = DST_R_NOENGINE;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein goto cleanup_rm;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein re = ENGINE_get_default_RAND();
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (re == NULL) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein re = ENGINE_new();
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (re == NULL) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein result = ISC_R_NOMEMORY;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein goto cleanup_rm;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein }
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein ENGINE_set_RAND(re, rm);
ENGINE_set_default_RAND(re);
ENGINE_free(re);
} else
ENGINE_finish(re);
#else
RAND_set_rand_method(rm);
#endif /* USE_ENGINE */
return (ISC_R_SUCCESS);
#ifdef USE_ENGINE
cleanup_rm:
if (e != NULL)
ENGINE_free(e);
e = NULL;
mem_free(rm FILELINE);
rm = NULL;
#endif
cleanup_mutexinit:
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
CRYPTO_set_locking_callback(NULL);
DESTROYMUTEXBLOCK(locks, nlocks);
cleanup_mutexalloc:
mem_free(locks FILELINE);
locks = NULL;
#endif
return (result);
}
void
dst__openssl_destroy(void) {
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OPENSSL_cleanup();
if (rm != NULL) {
mem_free(rm FILELINE);
rm = NULL;
}
#else
/*
* Sequence taken from apps_shutdown() in <apps/apps.h>.
*/
if (rm != NULL) {
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
RAND_cleanup();
#endif
mem_free(rm FILELINE);
rm = NULL;
}
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
CONF_modules_free();
#endif
OBJ_cleanup();
EVP_cleanup();
#if defined(USE_ENGINE)
if (e != NULL)
ENGINE_free(e);
e = NULL;
#if defined(USE_ENGINE) && OPENSSL_VERSION_NUMBER >= 0x00907000L
ENGINE_cleanup();
#endif
#endif
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
CRYPTO_cleanup_all_ex_data();
#endif
ERR_clear_error();
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
ERR_remove_state(0);
#endif
ERR_free_strings();
#ifdef DNS_CRYPTO_LEAKS
CRYPTO_mem_leaks_fp(stderr);
#endif
if (locks != NULL) {
CRYPTO_set_locking_callback(NULL);
DESTROYMUTEXBLOCK(locks, nlocks);
mem_free(locks FILELINE);
locks = NULL;
}
#endif
}
static isc_result_t
toresult(isc_result_t fallback) {
isc_result_t result = fallback;
unsigned long err = ERR_get_error();
#if defined(HAVE_OPENSSL_ECDSA) && \
defined(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
int lib = ERR_GET_LIB(err);
#endif
int reason = ERR_GET_REASON(err);
switch (reason) {
/*
* ERR_* errors are globally unique; others
* are unique per sublibrary
*/
case ERR_R_MALLOC_FAILURE:
result = ISC_R_NOMEMORY;
break;
default:
#if defined(HAVE_OPENSSL_ECDSA) && \
defined(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED)
if (lib == ERR_R_ECDSA_LIB &&
reason == ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED) {
result = ISC_R_NOENTROPY;
break;
}
#endif
break;
}
return (result);
}
isc_result_t
dst__openssl_toresult(isc_result_t fallback) {
isc_result_t result;
result = toresult(fallback);
ERR_clear_error();
return (result);
}
isc_result_t
dst__openssl_toresult2(const char *funcname, isc_result_t fallback) {
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) {
isc_result_t result;
unsigned long err;
const char *file, *data;
int line, flags;
char buf[256];
result = toresult(fallback);
isc_log_write(dns_lctx, category,
DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING,
"%s failed (%s)", funcname,
isc_result_totext(result));
if (result == ISC_R_NOMEMORY)
goto done;
for (;;) {
err = ERR_get_error_line_data(&file, &line, &data, &flags);
if (err == 0U)
goto done;
ERR_error_string_n(err, buf, sizeof(buf));
isc_log_write(dns_lctx, category,
DNS_LOGMODULE_CRYPTO, ISC_LOG_INFO,
"%s:%s:%d:%s", buf, file, line,
(flags & ERR_TXT_STRING) ? data : "");
}
done:
ERR_clear_error();
return (result);
}
#if defined(USE_ENGINE)
ENGINE *
dst__openssl_getengine(const char *engine) {
if (engine == NULL)
return (NULL);
if (e == NULL)
return (NULL);
if (strcmp(engine, ENGINE_get_id(e)) == 0)
return (e);
return (NULL);
}
#endif
#else /* OPENSSL */
#include <isc/util.h>
EMPTY_TRANSLATION_UNIT
#endif /* OPENSSL */
/*! \file */