Skip to content

Commit

Permalink
Release the drbg in the global default context before engines
Browse files Browse the repository at this point in the history
Fixes #17995
Fixes #18578

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from #19390)
  • Loading branch information
t8m committed Nov 10, 2022
1 parent aa97297 commit d0f8056
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
15 changes: 15 additions & 0 deletions crypto/context.c
Expand Up @@ -15,6 +15,7 @@
#include "internal/bio.h"
#include "internal/provider.h"
#include "crypto/ctype.h"
#include "crypto/rand.h"

struct ossl_lib_ctx_onfree_list_st {
ossl_lib_ctx_onfree_fn *fn;
Expand Down Expand Up @@ -271,6 +272,20 @@ OSSL_LIB_CTX *OSSL_LIB_CTX_set0_default(OSSL_LIB_CTX *libctx)

return NULL;
}

void ossl_release_default_drbg_ctx(void)
{
int dynidx = default_context_int.dyn_indexes[OSSL_LIB_CTX_DRBG_INDEX];

/* early release of the DRBG in global default libctx, no locking */
if (dynidx != -1) {
void *data;

data = CRYPTO_get_ex_data(&default_context_int.data, dynidx);
ossl_rand_ctx_free(data);
CRYPTO_set_ex_data(&default_context_int.data, dynidx, NULL);
}
}
#endif

OSSL_LIB_CTX *ossl_lib_ctx_get_concrete(OSSL_LIB_CTX *ctx)
Expand Down
5 changes: 3 additions & 2 deletions crypto/rand/rand_lib.c
Expand Up @@ -96,6 +96,7 @@ void ossl_rand_cleanup_int(void)
CRYPTO_THREAD_lock_free(rand_meth_lock);
rand_meth_lock = NULL;
# endif
ossl_release_default_drbg_ctx();
rand_inited = 0;
}

Expand Down Expand Up @@ -469,7 +470,7 @@ static void *rand_ossl_ctx_new(OSSL_LIB_CTX *libctx)
return NULL;
}

static void rand_ossl_ctx_free(void *vdgbl)
void ossl_rand_ctx_free(void *vdgbl)
{
RAND_GLOBAL *dgbl = vdgbl;

Expand All @@ -494,7 +495,7 @@ static void rand_ossl_ctx_free(void *vdgbl)
static const OSSL_LIB_CTX_METHOD rand_drbg_ossl_ctx_method = {
OSSL_LIB_CTX_METHOD_PRIORITY_2,
rand_ossl_ctx_new,
rand_ossl_ctx_free,
ossl_rand_ctx_free,
};

static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx)
Expand Down
1 change: 1 addition & 0 deletions include/crypto/rand.h
Expand Up @@ -125,4 +125,5 @@ void ossl_rand_cleanup_nonce(ossl_unused const OSSL_CORE_HANDLE *handle,
size_t ossl_pool_acquire_entropy(RAND_POOL *pool);
int ossl_pool_add_nonce_data(RAND_POOL *pool);

void ossl_rand_ctx_free(void *vdgbl);
#endif
2 changes: 2 additions & 0 deletions include/internal/cryptlib.h
Expand Up @@ -199,6 +199,8 @@ int ossl_lib_ctx_run_once(OSSL_LIB_CTX *ctx, unsigned int idx,
int ossl_lib_ctx_onfree(OSSL_LIB_CTX *ctx, ossl_lib_ctx_onfree_fn onfreefn);
const char *ossl_lib_ctx_get_descriptor(OSSL_LIB_CTX *libctx);

void ossl_release_default_drbg_ctx(void);

OSSL_LIB_CTX *ossl_crypto_ex_data_get_ossl_lib_ctx(const CRYPTO_EX_DATA *ad);
int ossl_crypto_new_ex_data_ex(OSSL_LIB_CTX *ctx, int class_index, void *obj,
CRYPTO_EX_DATA *ad);
Expand Down

0 comments on commit d0f8056

Please sign in to comment.