Commit b184e3ef authored by Matt Caswell's avatar Matt Caswell
Browse files

Provide framework for auto initialise/deinitialise of the library



This commit provides the basis and core code for an auto initialisation
and deinitialisation framework for libcrypto and libssl. The intention is
to remove the need (in many circumstances) to call explicit initialise and
deinitialise functions. Explicit initialisation will still be an option,
and if non-default initialisation is needed then it will be required.
Similarly for de-initialisation (although this will be a lot easier since
it will bring all de-initialisation into a single function).

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
parent bc66265d
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -227,6 +227,7 @@ my @disablables = (
    "aes",
    "asm",
    "async",
    "autoalginit",
    "bf",
    "camellia",
    "capieng",
@@ -741,7 +742,8 @@ foreach (sort (keys %disabled))
		my ($ALGO, $algo);
		($ALGO = $algo = $_) =~ tr/[\-a-z]/[_A-Z]/;

		if (/^asm$/ || /^err$/ || /^hw$/ || /^hw-/ || /^async$/)
		if (/^asm$/ || /^err$/ || /^hw$/ || /^hw-/ || /^async$/
				|| /^autoalginit/)
			{
			push @{$config{openssl_other_defines}}, "OPENSSL_NO_$ALGO";
			print " OPENSSL_NO_$ALGO";
+2 −2
Original line number Diff line number Diff line
@@ -34,10 +34,10 @@ LIB= $(TOP)/libcrypto.a
SHARED_LIB= libcrypto$(SHLIB_EXT)
LIBSRC=	cryptlib.c mem.c mem_clr.c mem_dbg.c cversion.c ex_data.c cpt_err.c \
	ebcdic.c uid.c o_time.c o_str.c o_dir.c thr_id.c lock.c fips_ers.c \
	o_init.c o_fips.c mem_sec.c
	o_init.c o_fips.c mem_sec.c init.c
LIBOBJ= cryptlib.o mem.o mem_dbg.o cversion.o ex_data.o cpt_err.o \
	ebcdic.o uid.o o_time.o o_str.o o_dir.o thr_id.o lock.o fips_ers.o \
	o_init.o o_fips.o mem_sec.o $(CPUID_OBJ)
	o_init.o o_fips.o mem_sec.o init.o $(CPUID_OBJ)

SRC= $(LIBSRC)

+79 −0
Original line number Diff line number Diff line
/*
 * Written by Matt Caswell for the OpenSSL project.
 */
/* ====================================================================
 * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <internal/cryptlib.h>

/* This file is not scanned by mkdef.pl, whereas cryptlib.h is */

struct thread_local_inits_st {
    int async;
    int err_state;
};
void *ossl_init_get_thread_local(int alloc);
int ossl_init_thread_start(uint64_t opts);
void ossl_init_thread_stop(struct thread_local_inits_st *locals);
/*
 * OPENSSL_INIT flags. The primary list of these is in crypto.h. Flags below
 * are those ommitted from crypto.h because they are "reserverd for internal
 * use".
 */
# define OPENSSL_INIT_ZLIB                   0x010000

/* OPENSSL_INIT_THREAD flags */
# define OPENSSL_INIT_THREAD_ASYNC           0x01
# define OPENSSL_INIT_THREAD_ERR_STATE       0x02

crypto/init.c

0 → 100644
+775 −0

File added.

Preview size limit exceeded, changes collapsed.

+55 −0
Original line number Diff line number Diff line
@@ -546,6 +546,61 @@ int CRYPTO_memcmp(const volatile void * volatile in_a,
                  const volatile void * volatile in_b,
                  size_t len);

/* Standard initialisation options */
# define OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS 0x000001
# define OPENSSL_INIT_LOAD_CRYPTO_STRINGS    0x000002
# define OPENSSL_INIT_ADD_ALL_CIPHERS        0x000004
# define OPENSSL_INIT_ADD_ALL_DIGESTS        0x000008
# define OPENSSL_INIT_NO_ADD_ALL_CIPHERS     0x000010
# define OPENSSL_INIT_NO_ADD_ALL_DIGESTS     0x000020
# define OPENSSL_INIT_LOAD_CONFIG            0x000040
# define OPENSSL_INIT_NO_LOAD_CONFIG         0x000080
# define OPENSSL_INIT_ASYNC                  0x000100
# define OPENSSL_INIT_ENGINE_RDRAND          0x000200
# define OPENSSL_INIT_ENGINE_DYNAMIC         0x000400
# define OPENSSL_INIT_ENGINE_OPENSSL         0x000800
# define OPENSSL_INIT_ENGINE_CRYPTODEV       0x001000
# define OPENSSL_INIT_ENGINE_CAPI            0x002000
# define OPENSSL_INIT_ENGINE_PADLOCK         0x004000
# define OPENSSL_INIT_ENGINE_DASYNC          0x008000
/* OPENSSL_INIT flag 0x010000 reserved for internal use */
/* Max OPENSSL_INIT flag value is 0x80000000 */

/* openssl and dasync not counted as builtin */
# define OPENSSL_INIT_ENGINE_ALL_BUILTIN \
    (OPENSSL_INIT_ENGINE_RDRAND | OPENSSL_INIT_ENGINE_DYNAMIC \
    | OPENSSL_INIT_ENGINE_CRYPTODEV | OPENSSL_INIT_ENGINE_CAPI | \
    OPENSSL_INIT_ENGINE_PADLOCK)



/* Optional settings for initialisation */
# define OPENSSL_INIT_SET_END                0
# define OPENSSL_INIT_SET_CONF_FILENAME      1

typedef struct ossl_init_settings_st {
    int name;
    union {
        int type_int;
        long type_long;
        int32_t type_int32_t;
        uint32_t type_uint32_t;
        int64_t type_int64_t;
        uint64_t type_uint64_t;
        size_t type_size_t;
        const char *type_string;
        void *type_void_ptr;
    } value;
} OPENSSL_INIT_SETTINGS;

typedef struct ossl_init_stop_st OPENSSL_INIT_STOP;

/* Library initialisation functions */
void OPENSSL_INIT_library_stop(void);
void OPENSSL_INIT_crypto_library_start(uint64_t opts,
                                       const OPENSSL_INIT_SETTINGS *settings);
int OPENSSL_INIT_register_stop_handler(void (*handler)(void));

/* BEGIN ERROR CODES */
/*
 * The following lines are auto generated by the script mkerr.pl. Any changes
Loading