ad->sk = 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;
+
+ curval = CRYPTO_get_ex_data(ad, idx);
+
+ /* Already there, no need to allocate */
+ if (curval != NULL)
+ return 1;
+
+ ip = get_and_lock(class_index);
+ f = sk_EX_CALLBACK_value(ip->meth, idx);
+ CRYPTO_THREAD_unlock(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.
+ */
+ 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
=head1 NAME
CRYPTO_EX_new, CRYPTO_EX_free, CRYPTO_EX_dup,
-CRYPTO_free_ex_index, CRYPTO_get_ex_new_index, CRYPTO_set_ex_data,
-CRYPTO_get_ex_data, CRYPTO_free_ex_data, CRYPTO_new_ex_data
+CRYPTO_free_ex_index, CRYPTO_get_ex_new_index,
+CRYPTO_alloc_ex_data, CRYPTO_set_ex_data, CRYPTO_get_ex_data,
+CRYPTO_free_ex_data, CRYPTO_new_ex_data
- functions supporting application-specific data
=head1 SYNOPSIS
int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)
+ int CRYPTO_alloc_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad,
+ int idx);
+
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *r, int idx, void *arg);
void *CRYPTO_get_ex_data(CRYPTO_EX_DATA *r, int idx);
that the entire parent, or containing, structure has been set up.
The new_func() is typically used only to allocate memory to store the
exdata, and perhaps an "initialized" flag within that memory.
-The exdata value should be set by calling CRYPTO_set_ex_data().
+The exdata value may be allocated later on with CRYPTO_alloc_ex_data(),
+or may be set by calling CRYPTO_set_ex_data().
When a structure is free'd (such as SSL_CTX_free()) then the
free_func() is called for every defined index. Again, the state of the
CRYPTO_get_ex_new_index() returns a new index or -1 on failure.
-CRYPTO_free_ex_index() and
-CRYPTO_set_ex_data() return 1 on success or 0 on failure.
+CRYPTO_free_ex_index(), CRYPTO_alloc_ex_data() and CRYPTO_set_ex_data()
+return 1 on success or 0 on failure.
CRYPTO_get_ex_data() returns the application data or NULL on failure;
note that NULL may be a valid value.
dup_func() should return 0 for failure and 1 for success.
+=head1 HISTORY
+
+CRYPTO_alloc_ex_data() was added in OpenSSL 3.0.0.
+
=head1 COPYRIGHT
Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
static void *saved_argp;
static int saved_idx;
static int saved_idx2;
+static int saved_idx3;
static int gbl_result;
/*
int idx, long argl, void *argp)
{
MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data));
- if (!TEST_int_eq(idx, saved_idx2)
+
+ if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
|| !TEST_long_eq(argl, saved_argl)
|| !TEST_ptr_eq(argp, saved_argp)
|| !TEST_ptr_null(ptr)
|| !TEST_ptr(ex_data)
- || !TEST_true(CRYPTO_set_ex_data(ad, saved_idx2, ex_data))) {
+ || !TEST_true(CRYPTO_set_ex_data(ad, idx, ex_data))) {
gbl_result = 0;
OPENSSL_free(ex_data);
} else {
void *from_d, int idx, long argl, void *argp)
{
MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
- MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(to, saved_idx2);
- if (!TEST_int_eq(idx, saved_idx2)
+ MYOBJ_EX_DATA *ex_data = NULL;
+
+ if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
|| !TEST_long_eq(argl, saved_argl)
|| !TEST_ptr_eq(argp, saved_argp)
|| !TEST_ptr(from_d)
|| !TEST_ptr(*update_ex_data)
- || !TEST_ptr(ex_data)
+ || !TEST_ptr(ex_data = CRYPTO_get_ex_data(to, idx))
|| !TEST_true(ex_data->new)) {
gbl_result = 0;
} else {
static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
int idx, long argl, void *argp)
{
- MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, saved_idx2);
- OPENSSL_free(ex_data);
- if (!TEST_int_eq(idx, saved_idx2)
+ MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, idx);
+
+ if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
|| !TEST_long_eq(argl, saved_argl)
|| !TEST_ptr_eq(argp, saved_argp)
- || !TEST_ptr(ex_data)
- || !TEST_true(CRYPTO_set_ex_data(ad, saved_idx2, NULL)))
+ || !TEST_true(CRYPTO_set_ex_data(ad, idx, NULL)))
gbl_result = 0;
+ OPENSSL_free(ex_data);
}
typedef struct myobj_st {
static void MYOBJ_sethello2(MYOBJ *obj, char *cp)
{
MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
+
if (TEST_ptr(ex_data))
ex_data->hello = cp;
else
static char *MYOBJ_gethello2(MYOBJ *obj)
{
MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
+
+ if (TEST_ptr(ex_data))
+ return ex_data->hello;
+
+ obj->st = gbl_result = 0;
+ return NULL;
+}
+
+static void MYOBJ_allochello3(MYOBJ *obj, char *cp)
+{
+ MYOBJ_EX_DATA* ex_data = NULL;
+
+ if (TEST_ptr_null(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3))
+ && TEST_true(CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_APP, obj,
+ &obj->ex_data, saved_idx3))
+ && TEST_ptr(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3)))
+ ex_data->hello = cp;
+ else
+ obj->st = gbl_result = 0;
+}
+
+static char *MYOBJ_gethello3(MYOBJ *obj)
+{
+ MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3);
+
if (TEST_ptr(ex_data))
return ex_data->hello;
return 0;
if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
return 0;
- if (!TEST_ptr(CRYPTO_get_ex_data(&t2->ex_data, saved_idx2)))
+
+ /*
+ * saved_idx3 differs from other indexes by being created after the exdata
+ * was initialized.
+ */
+ saved_idx3 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
+ saved_argl, saved_argp,
+ exnew2, exdup2, exfree2);
+ if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3)))
return 0;
MYOBJ_sethello(t1, p);
if (!TEST_ptr_eq(cp, p))
return 0;
+ MYOBJ_allochello3(t1, p);
+ cp = MYOBJ_gethello3(t1);
+ if (!TEST_ptr_eq(cp, p))
+ return 0;
+
cp = MYOBJ_gethello(t2);
if (!TEST_ptr_null(cp))
return 0;
if (!TEST_ptr_eq(cp, p))
return 0;
+ cp = MYOBJ_gethello3(t3);
+ if (!TEST_ptr_eq(cp, p))
+ return 0;
+
MYOBJ_free(t1);
MYOBJ_free(t2);
MYOBJ_free(t3);