}
static const OSSL_LIB_CTX_METHOD bio_core_globals_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
bio_core_globals_new,
bio_core_globals_free,
};
idx = ossl_crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OSSL_LIB_CTX, 0,
(void *)meth,
ossl_lib_ctx_generic_new,
- NULL, ossl_lib_ctx_generic_free);
+ NULL, ossl_lib_ctx_generic_free,
+ meth->priority);
if (idx < 0)
return 0;
}
static const OSSL_LIB_CTX_METHOD stored_namemap_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
stored_namemap_new,
stored_namemap_free,
};
static const OSSL_LIB_CTX_METHOD decoder_store_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
decoder_store_new,
decoder_store_free,
};
static const OSSL_LIB_CTX_METHOD encoder_store_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
encoder_store_new,
encoder_store_free,
};
static const OSSL_LIB_CTX_METHOD evp_method_store_method = {
+ /* We want evp_method_store to be cleaned up before the provider store */
+ OSSL_LIB_CTX_METHOD_HIGH_PRIORITY,
evp_method_store_new,
evp_method_store_free,
};
* https://www.openssl.org/source/license.html
*/
+#include <stdlib.h>
#include "crypto/cryptlib.h"
#include "internal/thread_once.h"
long argl, void *argp,
CRYPTO_EX_new *new_func,
CRYPTO_EX_dup *dup_func,
- CRYPTO_EX_free *free_func)
+ CRYPTO_EX_free *free_func,
+ int priority)
{
int toret = -1;
EX_CALLBACK *a;
a->new_func = new_func;
a->dup_func = dup_func;
a->free_func = free_func;
+ a->priority = priority;
if (!sk_EX_CALLBACK_push(ip->meth, NULL)) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
CRYPTO_EX_free *free_func)
{
return ossl_crypto_get_ex_new_index_ex(NULL, class_index, argl, argp,
- new_func, dup_func, free_func);
+ new_func, dup_func, free_func, 0);
}
/*
return toret;
}
+struct ex_callback_entry {
+ const EX_CALLBACK *excb;
+ int index;
+};
+
+static int ex_callback_compare(const void *a, const void *b)
+{
+ const struct ex_callback_entry *ap = (const struct ex_callback_entry *)a;
+ const struct ex_callback_entry *bp = (const struct ex_callback_entry *)b;
+
+ if (ap->excb == bp->excb)
+ return 0;
+
+ if (ap->excb == NULL)
+ return 1;
+ if (bp->excb == NULL)
+ return -1;
+ if (ap->excb->priority == bp->excb->priority)
+ return 0;
+ return ap->excb->priority > bp->excb->priority ? -1 : 1;
+}
/*
* Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
int mx, i;
EX_CALLBACKS *ip;
void *ptr;
- EX_CALLBACK *f;
- EX_CALLBACK *stack[10];
- EX_CALLBACK **storage = NULL;
+ const EX_CALLBACK *f;
+ struct ex_callback_entry stack[10];
+ struct ex_callback_entry *storage = NULL;
OSSL_EX_DATA_GLOBAL *global = ossl_lib_ctx_get_ex_data_global(ad->ctx);
if (global == NULL)
else
storage = OPENSSL_malloc(sizeof(*storage) * mx);
if (storage != NULL)
- for (i = 0; i < mx; i++)
- storage[i] = sk_EX_CALLBACK_value(ip->meth, i);
+ for (i = 0; i < mx; i++) {
+ storage[i].excb = sk_EX_CALLBACK_value(ip->meth, i);
+ storage[i].index = i;
+ }
}
CRYPTO_THREAD_unlock(global->ex_data_lock);
- for (i = 0; i < mx; i++) {
- if (storage != NULL)
- f = storage[i];
- else {
- if (!CRYPTO_THREAD_write_lock(global->ex_data_lock))
- continue;
- f = sk_EX_CALLBACK_value(ip->meth, i);
- CRYPTO_THREAD_unlock(global->ex_data_lock);
- }
- if (f != NULL && f->free_func != NULL) {
- ptr = CRYPTO_get_ex_data(ad, i);
- f->free_func(obj, ptr, ad, i, f->argl, f->argp);
+ if (storage != NULL) {
+ /* Sort according to priority. High priority first */
+ qsort(storage, mx, sizeof(*storage), ex_callback_compare);
+ for (i = 0; i < mx; i++) {
+ f = storage[i].excb;
+
+ if (f != NULL && f->free_func != NULL) {
+ ptr = CRYPTO_get_ex_data(ad, storage[i].index);
+ f->free_func(obj, ptr, ad, storage[i].index, f->argl, f->argp);
+ }
}
}
}
static const OSSL_LIB_CTX_METHOD thread_event_ossl_ctx_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
thread_event_ossl_ctx_new,
thread_event_ossl_ctx_free,
};
}
static const OSSL_LIB_CTX_METHOD property_defns_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
property_defns_new,
property_defns_free,
};
static const OSSL_LIB_CTX_METHOD ossl_ctx_global_properties_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
ossl_ctx_global_properties_new,
ossl_ctx_global_properties_free,
};
}
static const OSSL_LIB_CTX_METHOD property_string_data_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
property_string_data_new,
property_string_data_free,
};
}
static const OSSL_LIB_CTX_METHOD provider_conf_ossl_ctx_method = {
+ /* Must be freed before the provider store is freed */
+ OSSL_LIB_CTX_METHOD_HIGH_PRIORITY,
prov_conf_ossl_ctx_new,
prov_conf_ossl_ctx_free,
};
}
static const OSSL_LIB_CTX_METHOD provider_store_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
provider_store_new,
provider_store_free,
};
}
static const OSSL_LIB_CTX_METHOD rand_drbg_ossl_ctx_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
rand_ossl_ctx_new,
rand_ossl_ctx_free,
};
}
static const OSSL_LIB_CTX_METHOD self_test_set_callback_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
self_test_set_callback_new,
self_test_set_callback_free,
};
static const OSSL_LIB_CTX_METHOD loader_store_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
loader_store_new,
loader_store_free,
};
struct ex_callback_st {
long argl; /* Arbitrary long */
void *argp; /* Arbitrary void * */
+ int priority; /* Priority ordering for freeing */
CRYPTO_EX_new *new_func;
CRYPTO_EX_free *free_func;
CRYPTO_EX_dup *dup_func;
# define OSSL_LIB_CTX_BIO_CORE_INDEX 17
# define OSSL_LIB_CTX_MAX_INDEXES 18
+# define OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY 0
+# define OSSL_LIB_CTX_METHOD_HIGH_PRIORITY 1
typedef struct ossl_lib_ctx_method {
+ int priority;
void *(*new_func)(OSSL_LIB_CTX *ctx);
void (*free_func)(void *);
} OSSL_LIB_CTX_METHOD;
long argl, void *argp,
CRYPTO_EX_new *new_func,
CRYPTO_EX_dup *dup_func,
- CRYPTO_EX_free *free_func);
+ CRYPTO_EX_free *free_func,
+ int priority);
int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx);
/* Function for simple binary search */
}
static const OSSL_LIB_CTX_METHOD fips_prov_ossl_ctx_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
fips_prov_ossl_ctx_new,
fips_prov_ossl_ctx_free,
};
}
static const OSSL_LIB_CTX_METHOD rand_crng_ossl_ctx_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
rand_crng_ossl_ctx_new,
rand_crng_ossl_ctx_free,
};
}
static const OSSL_LIB_CTX_METHOD drbg_nonce_ossl_ctx_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
prov_drbg_nonce_ossl_ctx_new,
prov_drbg_nonce_ossl_ctx_free,
};
OPENSSL_free(ptr);
}
static const OSSL_LIB_CTX_METHOD foo_method = {
+ OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
foo_new,
foo_free
};