From d64b62998beceef8a8b8e1558862eb1c671d677a Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Wed, 6 Feb 2019 17:42:50 +0100 Subject: Add an OpenSSL library context The context builds on CRYPTO_EX_DATA, allowing it to be dynamically extended with new data from the different parts of libcrypto. Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/8225) --- crypto/build.info | 2 +- crypto/context.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 crypto/context.c (limited to 'crypto') diff --git a/crypto/build.info b/crypto/build.info index 3b6731534b..fc0050ea4e 100644 --- a/crypto/build.info +++ b/crypto/build.info @@ -12,7 +12,7 @@ SOURCE[../libcrypto]=\ cryptlib.c mem.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 o_fopen.c ctype.c \ threads_pthread.c threads_win.c threads_none.c getenv.c \ - o_init.c o_fips.c mem_sec.c init.c sparse_array.c \ + o_init.c o_fips.c mem_sec.c init.c context.c sparse_array.c \ {- $target{cpuid_asm_src} -} {- $target{uplink_aux_src} -} DEPEND[cversion.o]=buildinf.h diff --git a/crypto/context.c b/crypto/context.c new file mode 100644 index 0000000000..752711b9a4 --- /dev/null +++ b/crypto/context.c @@ -0,0 +1,110 @@ +/* + * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include "internal/cryptlib.h" +#include "internal/thread_once.h" + +struct openssl_ctx_st { + CRYPTO_RWLOCK *lock; + CRYPTO_EX_DATA data; +}; + +static OPENSSL_CTX default_context; + +static int context_init(OPENSSL_CTX *ctx) +{ + return (ctx->lock = CRYPTO_THREAD_lock_new()) != NULL + && CRYPTO_new_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, + &ctx->data); +} + +static int context_deinit(OPENSSL_CTX *ctx) +{ + CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data); + CRYPTO_THREAD_lock_free(ctx->lock); + return 1; +} + +static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT; +static void do_default_context_deinit(void) +{ + context_deinit(&default_context); +} +DEFINE_RUN_ONCE_STATIC(do_default_context_init) +{ + return OPENSSL_init_crypto(0, NULL) + && context_init(&default_context) + && OPENSSL_atexit(do_default_context_deinit); +} + +OPENSSL_CTX *OPENSSL_CTX_new(void) +{ + OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); + + if (ctx != NULL && !context_init(ctx)) { + OPENSSL_CTX_free(ctx); + ctx = NULL; + } + return ctx; +} + +void OPENSSL_CTX_free(OPENSSL_CTX *ctx) +{ + if (ctx != NULL) + context_deinit(ctx); + OPENSSL_free(ctx); +} + +static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign, + CRYPTO_EX_DATA *ad, int index, + long argl_ign, void *argp) +{ + const OPENSSL_CTX_METHOD *meth = argp; + void *ptr = meth->new_func(); + + if (ptr != NULL) + CRYPTO_set_ex_data(ad, index, ptr); +} +static void openssl_ctx_generic_free(void *parent_ign, void *ptr, + CRYPTO_EX_DATA *ad, int index, + long argl_ign, void *argp) +{ + const OPENSSL_CTX_METHOD *meth = argp; + + meth->free_func(ptr); +} +int openssl_ctx_new_index(const OPENSSL_CTX_METHOD *meth) +{ + return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_OPENSSL_CTX, 0, (void *)meth, + openssl_ctx_generic_new, NULL, + openssl_ctx_generic_free); +} + +void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index) +{ + void *data = NULL; + + if (ctx == NULL) { + if (!RUN_ONCE(&default_context_init, do_default_context_init)) + return 0; + ctx = &default_context; + } + + CRYPTO_THREAD_read_lock(ctx->lock); + + /* The alloc call ensures there's a value there */ + if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, + &ctx->data, index)) + data = CRYPTO_get_ex_data(&ctx->data, index); + + CRYPTO_THREAD_unlock(ctx->lock); + + return data; +} + -- cgit v1.2.3