| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:33 +08:00
										 |  |  | #include "crypto/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-20 18:25:39 +08:00
										 |  |  | #include <openssl/conf.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | #include "internal/thread_once.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-14 22:00:35 +08:00
										 |  |  | #include "internal/property.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  | struct openssl_ctx_onfree_list_st { | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     openssl_ctx_onfree_fn *fn; | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |     struct openssl_ctx_onfree_list_st *next; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | struct openssl_ctx_st { | 
					
						
							|  |  |  |     CRYPTO_RWLOCK *lock; | 
					
						
							|  |  |  |     CRYPTO_EX_DATA data; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * For most data in the OPENSSL_CTX we just use ex_data to store it. But | 
					
						
							|  |  |  |      * that doesn't work for ex_data itself - so we store that directly. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     OSSL_EX_DATA_GLOBAL global; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Map internal static indexes to dynamically created indexes */ | 
					
						
							|  |  |  |     int dyn_indexes[OPENSSL_CTX_MAX_INDEXES]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     /* Keep a separate lock for each index */ | 
					
						
							|  |  |  |     CRYPTO_RWLOCK *index_locks[OPENSSL_CTX_MAX_INDEXES]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     CRYPTO_RWLOCK *oncelock; | 
					
						
							|  |  |  |     int run_once_done[OPENSSL_CTX_MAX_RUN_ONCE]; | 
					
						
							|  |  |  |     int run_once_ret[OPENSSL_CTX_MAX_RUN_ONCE]; | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |     struct openssl_ctx_onfree_list_st *onfreelist; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | #ifndef FIPS_MODE
 | 
					
						
							|  |  |  | static OPENSSL_CTX default_context_int; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Always points at default_context_int if it has been initialised */ | 
					
						
							|  |  |  | static OPENSSL_CTX *default_context = NULL; | 
					
						
							| 
									
										
										
										
											2019-04-10 22:01:40 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int context_init(OPENSSL_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     size_t i; | 
					
						
							| 
									
										
										
										
											2019-08-14 22:00:35 +08:00
										 |  |  |     int exdata_done = 0; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx->lock = CRYPTO_THREAD_lock_new(); | 
					
						
							|  |  |  |     if (ctx->lock == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->oncelock = CRYPTO_THREAD_lock_new(); | 
					
						
							|  |  |  |     if (ctx->oncelock == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++) { | 
					
						
							|  |  |  |         ctx->index_locks[i] = CRYPTO_THREAD_lock_new(); | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |         ctx->dyn_indexes[i] = -1; | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |         if (ctx->index_locks[i] == NULL) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 22:00:35 +08:00
										 |  |  |     /* OPENSSL_CTX is built on top of ex_data so we initialise that directly */ | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     if (!do_ex_data_init(ctx)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2019-08-14 22:00:35 +08:00
										 |  |  |     exdata_done = 1; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, | 
					
						
							|  |  |  |                                &ctx->data)) { | 
					
						
							|  |  |  |         crypto_cleanup_all_ex_data_int(ctx); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 22:00:35 +08:00
										 |  |  |     /* Everything depends on properties, so we also pre-initialise that */ | 
					
						
							|  |  |  |     if (!ossl_property_parse_init(ctx)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2019-08-14 22:00:35 +08:00
										 |  |  |     if (exdata_done) | 
					
						
							|  |  |  |         crypto_cleanup_all_ex_data_int(ctx); | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     CRYPTO_THREAD_lock_free(ctx->oncelock); | 
					
						
							|  |  |  |     CRYPTO_THREAD_lock_free(ctx->lock); | 
					
						
							|  |  |  |     ctx->lock = NULL; | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int context_deinit(OPENSSL_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     struct openssl_ctx_onfree_list_st *tmp, *onfree; | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 23:31:27 +08:00
										 |  |  |     ossl_ctx_thread_stop(ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     onfree = ctx->onfreelist; | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |     while (onfree != NULL) { | 
					
						
							|  |  |  |         onfree->fn(ctx); | 
					
						
							|  |  |  |         tmp = onfree; | 
					
						
							|  |  |  |         onfree = onfree->next; | 
					
						
							|  |  |  |         OPENSSL_free(tmp); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  |     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data); | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     crypto_cleanup_all_ex_data_int(ctx); | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++) | 
					
						
							|  |  |  |         CRYPTO_THREAD_lock_free(ctx->index_locks[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     CRYPTO_THREAD_lock_free(ctx->oncelock); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  |     CRYPTO_THREAD_lock_free(ctx->lock); | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     ctx->lock = NULL; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | #ifndef FIPS_MODE
 | 
					
						
							|  |  |  | void openssl_ctx_default_deinit(void) | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     context_deinit(default_context); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | DEFINE_RUN_ONCE_STATIC(do_default_context_init) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     if (context_init(&default_context_int)) | 
					
						
							|  |  |  |         default_context = &default_context_int; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 18:25:39 +08:00
										 |  |  | #ifndef FIPS_MODE
 | 
					
						
							|  |  |  | int OPENSSL_CTX_load_config(OPENSSL_CTX *ctx, const char *config_file) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return CONF_modules_load_file_with_libctx(ctx, config_file, NULL, 0) > 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | void OPENSSL_CTX_free(OPENSSL_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ctx != NULL) | 
					
						
							|  |  |  |         context_deinit(ctx); | 
					
						
							|  |  |  |     OPENSSL_free(ctx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:58:08 +08:00
										 |  |  | OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifndef FIPS_MODE
 | 
					
						
							|  |  |  |     if (ctx == NULL) { | 
					
						
							|  |  |  |         if (!RUN_ONCE(&default_context_init, do_default_context_init)) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         return default_context; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return ctx; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 20:12:59 +08:00
										 |  |  | int openssl_ctx_is_default(OPENSSL_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifndef FIPS_MODE
 | 
					
						
							|  |  |  |     if (ctx == NULL || ctx == default_context) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad)); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Non-static so we can use it in context_internal_test */ | 
					
						
							|  |  |  | static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index, | 
					
						
							|  |  |  |                                   const OPENSSL_CTX_METHOD *meth) | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     int idx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:58:08 +08:00
										 |  |  |     ctx = openssl_ctx_get_concrete(ctx); | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0, | 
					
						
							|  |  |  |                                      (void *)meth, | 
					
						
							|  |  |  |                                      openssl_ctx_generic_new, | 
					
						
							|  |  |  |                                      NULL, openssl_ctx_generic_free); | 
					
						
							|  |  |  |     if (idx < 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->dyn_indexes[static_index] = idx; | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index, | 
					
						
							|  |  |  |                            const OPENSSL_CTX_METHOD *meth) | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     void *data = NULL; | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     int dynidx; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:58:08 +08:00
										 |  |  |     ctx = openssl_ctx_get_concrete(ctx); | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     CRYPTO_THREAD_read_lock(ctx->lock); | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     dynidx = ctx->dyn_indexes[index]; | 
					
						
							|  |  |  |     CRYPTO_THREAD_unlock(ctx->lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (dynidx != -1) { | 
					
						
							|  |  |  |         CRYPTO_THREAD_read_lock(ctx->index_locks[index]); | 
					
						
							|  |  |  |         data = CRYPTO_get_ex_data(&ctx->data, dynidx); | 
					
						
							|  |  |  |         CRYPTO_THREAD_unlock(ctx->index_locks[index]); | 
					
						
							|  |  |  |         return data; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     CRYPTO_THREAD_write_lock(ctx->index_locks[index]); | 
					
						
							|  |  |  |     CRYPTO_THREAD_write_lock(ctx->lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dynidx = ctx->dyn_indexes[index]; | 
					
						
							|  |  |  |     if (dynidx != -1) { | 
					
						
							|  |  |  |         CRYPTO_THREAD_unlock(ctx->lock); | 
					
						
							|  |  |  |         data = CRYPTO_get_ex_data(&ctx->data, dynidx); | 
					
						
							|  |  |  |         CRYPTO_THREAD_unlock(ctx->index_locks[index]); | 
					
						
							|  |  |  |         return data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!openssl_ctx_init_index(ctx, index, meth)) { | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |         CRYPTO_THREAD_unlock(ctx->lock); | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |         CRYPTO_THREAD_unlock(ctx->index_locks[index]); | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     CRYPTO_THREAD_unlock(ctx->lock); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  |     /* The alloc call ensures there's a value there */ | 
					
						
							|  |  |  |     if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |                              &ctx->data, ctx->dyn_indexes[index])) | 
					
						
							|  |  |  |         data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:43:11 +08:00
										 |  |  |     CRYPTO_THREAD_unlock(ctx->index_locks[index]); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  | OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-28 22:58:08 +08:00
										 |  |  |     ctx = openssl_ctx_get_concrete(ctx); | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     return &ctx->global; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  | int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx, | 
					
						
							|  |  |  |                          openssl_ctx_run_once_fn run_once_fn) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int done = 0, ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:58:08 +08:00
										 |  |  |     ctx = openssl_ctx_get_concrete(ctx); | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     CRYPTO_THREAD_read_lock(ctx->oncelock); | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |     done = ctx->run_once_done[idx]; | 
					
						
							|  |  |  |     if (done) | 
					
						
							|  |  |  |         ret = ctx->run_once_ret[idx]; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     CRYPTO_THREAD_unlock(ctx->oncelock); | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (done) | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     CRYPTO_THREAD_write_lock(ctx->oncelock); | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |     if (ctx->run_once_done[idx]) { | 
					
						
							|  |  |  |         ret = ctx->run_once_ret[idx]; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |         CRYPTO_THREAD_unlock(ctx->oncelock); | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = run_once_fn(ctx); | 
					
						
							|  |  |  |     ctx->run_once_done[idx] = 1; | 
					
						
							|  |  |  |     ctx->run_once_ret[idx] = ret; | 
					
						
							| 
									
										
										
										
											2019-05-01 18:02:43 +08:00
										 |  |  |     CRYPTO_THREAD_unlock(ctx->oncelock); | 
					
						
							| 
									
										
										
										
											2019-04-30 22:15:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct openssl_ctx_onfree_list_st *newonfree | 
					
						
							|  |  |  |         = OPENSSL_malloc(sizeof(*newonfree)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (newonfree == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     newonfree->fn = onfreefn; | 
					
						
							|  |  |  |     newonfree->next = ctx->onfreelist; | 
					
						
							|  |  |  |     ctx->onfreelist = newonfree; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } |