+ if (global == NULL)
+ return 0;
+
+ ip = get_and_lock(ctx, class_index);
+ if (ip == NULL)
+ return 0;
+
+ ad->ctx = ctx;
+ ad->sk = NULL;
+
+ mx = sk_EX_CALLBACK_num(ip->meth);
+ if (mx > 0) {
+ if (mx < (int)OSSL_NELEM(stack))
+ storage = stack;
+ 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);
+ }
+ CRYPTO_THREAD_unlock(global->ex_data_lock);
+
+ if (mx > 0 && storage == NULL) {
+ CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA_EX, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ for (i = 0; i < mx; i++) {
+ if (storage[i] != NULL && storage[i]->new_func != NULL) {
+ ptr = CRYPTO_get_ex_data(ad, i);
+ storage[i]->new_func(obj, ptr, ad, i,
+ storage[i]->argl, storage[i]->argp);
+ }
+ }
+ if (storage != stack)
+ OPENSSL_free(storage);
+ return 1;
+}
+
+int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
+{
+ return crypto_new_ex_data_ex(NULL, class_index, obj, ad);
+}
+
+/*
+ * Duplicate a CRYPTO_EX_DATA variable - including calling dup() callbacks
+ * for each index in the class used by this variable
+ */
+int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
+ const CRYPTO_EX_DATA *from)
+{
+ int mx, j, i;
+ void *ptr;
+ EX_CALLBACK *stack[10];
+ EX_CALLBACK **storage = NULL;
+ EX_CALLBACKS *ip;
+ int toret = 0;
+ OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(from->ctx);
+
+ if (global == NULL)
+ return 0;
+
+ to->ctx = from->ctx;
+ if (from->sk == NULL)
+ /* Nothing to copy over */
+ return 1;
+ if ((ip = get_and_lock(from->ctx, class_index)) == NULL)
+ return 0;
+
+ mx = sk_EX_CALLBACK_num(ip->meth);
+ j = sk_void_num(from->sk);
+ if (j < mx)
+ mx = j;
+ if (mx > 0) {
+ if (mx < (int)OSSL_NELEM(stack))
+ storage = stack;
+ 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);
+ }
+ CRYPTO_THREAD_unlock(global->ex_data_lock);
+
+ if (mx == 0)
+ return 1;
+ if (storage == NULL) {
+ CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ /*
+ * Make sure the ex_data stack is at least |mx| elements long to avoid
+ * issues in the for loop that follows; so go get the |mx|'th element
+ * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
+ * to itself. This is normally a no-op; but ensures the stack is the
+ * proper size
+ */
+ if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
+ goto err;
+
+ for (i = 0; i < mx; i++) {
+ ptr = CRYPTO_get_ex_data(from, i);
+ if (storage[i] != NULL && storage[i]->dup_func != NULL)
+ if (!storage[i]->dup_func(to, from, &ptr, i,
+ storage[i]->argl, storage[i]->argp))
+ goto err;
+ CRYPTO_set_ex_data(to, i, ptr);
+ }
+ toret = 1;
+ err:
+ if (storage != stack)
+ OPENSSL_free(storage);
+ return toret;
+}
+
+
+/*
+ * Cleanup a CRYPTO_EX_DATA variable - including calling free() callbacks for
+ * each index in the class used by this variable
+ */
+void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
+{
+ int mx, i;
+ EX_CALLBACKS *ip;
+ void *ptr;
+ EX_CALLBACK *f;
+ EX_CALLBACK *stack[10];
+ EX_CALLBACK **storage = NULL;
+ OSSL_EX_DATA_GLOBAL *global;
+
+ if ((ip = get_and_lock(ad->ctx, class_index)) == NULL)
+ goto err;
+ global = openssl_ctx_get_ex_data_global(ad->ctx);
+ if (global == NULL)
+ goto err;
+
+ mx = sk_EX_CALLBACK_num(ip->meth);
+ if (mx > 0) {
+ if (mx < (int)OSSL_NELEM(stack))
+ storage = stack;
+ 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);
+ }
+ CRYPTO_THREAD_unlock(global->ex_data_lock);
+
+ for (i = 0; i < mx; i++) {
+ if (storage != NULL)
+ f = storage[i];
+ else {
+ CRYPTO_THREAD_write_lock(global->ex_data_lock);
+ 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 != stack)
+ OPENSSL_free(storage);
+ err:
+ sk_void_free(ad->sk);
+ ad->sk = NULL;
+ ad->ctx = NULL;
+}
+
+/*
+ * Allocate a given CRYPTO_EX_DATA item using the class specific allocation
+ * function
+ */
+int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
+ int idx)
+{
+ EX_CALLBACK *f;
+ EX_CALLBACKS *ip;
+ void *curval;
+ OSSL_EX_DATA_GLOBAL *global = openssl_ctx_get_ex_data_global(ad->ctx);
+
+ if (global == NULL)
+ return 0;
+
+ curval = CRYPTO_get_ex_data(ad, idx);
+
+ /* Already there, no need to allocate */
+ if (curval != NULL)
+ return 1;
+
+ ip = get_and_lock(ad->ctx, class_index);
+ if (ip == NULL)
+ return 0;
+ f = sk_EX_CALLBACK_value(ip->meth, idx);
+ CRYPTO_THREAD_unlock(global->ex_data_lock);
+
+ /*
+ * This should end up calling CRYPTO_set_ex_data(), which allocates
+ * everything necessary to support placing the new data in the right spot.
+ */
+ if (f->new_func == NULL)
+ return 0;
+
+ f->new_func(obj, curval, ad, idx, f->argl, f->argp);
+
+ return 1;
+}
+
+/*
+ * For a given CRYPTO_EX_DATA variable, set the value corresponding to a
+ * particular index in the class used by this variable
+ */