diff options
author | Rich Salz <rsalz@akamai.com> | 2015-07-21 10:06:03 -0400 |
---|---|---|
committer | Rich Salz <rsalz@openssl.org> | 2015-12-01 11:48:37 -0500 |
commit | e6390acac925f952cfd06ccdbba0b273b8f71551 (patch) | |
tree | 219e865d2b4e0a2b362429e56829d5caf1b69ced /crypto | |
parent | d59c7c81e3850dc667d61047850c3b6936eb5fca (diff) | |
download | openssl-e6390acac925f952cfd06ccdbba0b273b8f71551.tar.gz |
ex_data part 2: doc fixes and CRYPTO_free_ex_index.
Add CRYPTO_free_ex_index (for shared libraries)
Unify and complete the documentation for all "ex_data" API's and objects.
Replace xxx_get_ex_new_index functions with a macro.
Added an exdata test.
Renamed the ex_data internal datatypes.
Reviewed-by: Matt Caswell <matt@openssl.org>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/bio/bio_lib.c | 7 | ||||
-rw-r--r-- | crypto/cpt_err.c | 3 | ||||
-rw-r--r-- | crypto/dh/dh_lib.c | 7 | ||||
-rw-r--r-- | crypto/dsa/dsa_lib.c | 7 | ||||
-rw-r--r-- | crypto/ecdh/ech_lib.c | 7 | ||||
-rw-r--r-- | crypto/ecdsa/ecs_lib.c | 7 | ||||
-rw-r--r-- | crypto/engine/eng_lib.c | 8 | ||||
-rw-r--r-- | crypto/ex_data.c | 141 | ||||
-rw-r--r-- | crypto/rsa/rsa_lib.c | 7 | ||||
-rw-r--r-- | crypto/store/str_lib.c | 7 | ||||
-rw-r--r-- | crypto/ui/ui_lib.c | 7 | ||||
-rw-r--r-- | crypto/x509/x509_vfy.c | 13 | ||||
-rw-r--r-- | crypto/x509/x_x509.c | 7 |
13 files changed, 97 insertions, 131 deletions
diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c index 0e3469d9cb..d8d01c7fe9 100644 --- a/crypto/bio/bio_lib.c +++ b/crypto/bio/bio_lib.c @@ -561,13 +561,6 @@ void BIO_copy_next_retry(BIO *b) b->retry_reason = b->next_bio->retry_reason; } -int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp, - new_func, dup_func, free_func); -} - int BIO_set_ex_data(BIO *bio, int idx, void *data) { return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data)); diff --git a/crypto/cpt_err.c b/crypto/cpt_err.c index 9f029cc455..b8d3fa4d7e 100644 --- a/crypto/cpt_err.c +++ b/crypto/cpt_err.c @@ -78,8 +78,9 @@ static ERR_STRING_DATA CRYPTO_str_functs[] = { {ERR_FUNC(CRYPTO_F_CRYPTO_NEW_EX_DATA), "CRYPTO_new_ex_data"}, {ERR_FUNC(CRYPTO_F_CRYPTO_SET_EX_DATA), "CRYPTO_set_ex_data"}, {ERR_FUNC(CRYPTO_F_DEF_ADD_INDEX), "DEF_ADD_INDEX"}, - {ERR_FUNC(CRYPTO_F_DEF_GET_CLASS), "def_get_class"}, + {ERR_FUNC(CRYPTO_F_DEF_GET_CLASS), "DEF_GET_CLASS"}, {ERR_FUNC(CRYPTO_F_FIPS_MODE_SET), "FIPS_mode_set"}, + {ERR_FUNC(CRYPTO_F_GET_AND_LOCK), "get_and_lock"}, {ERR_FUNC(CRYPTO_F_INT_DUP_EX_DATA), "INT_DUP_EX_DATA"}, {ERR_FUNC(CRYPTO_F_INT_FREE_EX_DATA), "INT_FREE_EX_DATA"}, {ERR_FUNC(CRYPTO_F_INT_NEW_EX_DATA), "INT_NEW_EX_DATA"}, diff --git a/crypto/dh/dh_lib.c b/crypto/dh/dh_lib.c index 49f82d867f..08aaa1ddb1 100644 --- a/crypto/dh/dh_lib.c +++ b/crypto/dh/dh_lib.c @@ -205,13 +205,6 @@ int DH_up_ref(DH *r) return ((i > 1) ? 1 : 0); } -int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, - new_func, dup_func, free_func); -} - int DH_set_ex_data(DH *d, int idx, void *arg) { return (CRYPTO_set_ex_data(&d->ex_data, idx, arg)); diff --git a/crypto/dsa/dsa_lib.c b/crypto/dsa/dsa_lib.c index b78a2bf5d6..4b02d770de 100644 --- a/crypto/dsa/dsa_lib.c +++ b/crypto/dsa/dsa_lib.c @@ -234,13 +234,6 @@ int DSA_size(const DSA *r) return (ret); } -int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, - new_func, dup_func, free_func); -} - int DSA_set_ex_data(DSA *d, int idx, void *arg) { return (CRYPTO_set_ex_data(&d->ex_data, idx, arg)); diff --git a/crypto/ecdh/ech_lib.c b/crypto/ecdh/ech_lib.c index 363d2fe23b..f402caf73c 100644 --- a/crypto/ecdh/ech_lib.c +++ b/crypto/ecdh/ech_lib.c @@ -200,13 +200,6 @@ ECDH_DATA *ecdh_check(EC_KEY *key) return ecdh_data; } -int ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDH, argl, argp, - new_func, dup_func, free_func); -} - int ECDH_set_ex_data(EC_KEY *d, int idx, void *arg) { ECDH_DATA *ecdh; diff --git a/crypto/ecdsa/ecs_lib.c b/crypto/ecdsa/ecs_lib.c index 4e1c63a175..ae516e748c 100644 --- a/crypto/ecdsa/ecs_lib.c +++ b/crypto/ecdsa/ecs_lib.c @@ -222,13 +222,6 @@ int ECDSA_size(const EC_KEY *r) return (ret); } -int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp, - new_func, dup_func, free_func); -} - int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg) { ECDSA_DATA *ecdsa; diff --git a/crypto/engine/eng_lib.c b/crypto/engine/eng_lib.c index 9ebb6943be..ed1034dc3b 100644 --- a/crypto/engine/eng_lib.c +++ b/crypto/engine/eng_lib.c @@ -212,14 +212,6 @@ void ENGINE_cleanup(void) /* Now the "ex_data" support */ -int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, - CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp, - new_func, dup_func, free_func); -} - int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg) { return (CRYPTO_set_ex_data(&e->ex_data, idx, arg)); diff --git a/crypto/ex_data.c b/crypto/ex_data.c index 29d8071eef..5bf8e1e5cd 100644 --- a/crypto/ex_data.c +++ b/crypto/ex_data.c @@ -112,48 +112,53 @@ #include <openssl/lhash.h> -typedef struct { + +/* + * Each structure type (sometimes called a class), that supports + * exdata has a stack of callbacks for each instance. + */ +typedef struct ex_callback_st { long argl; /* Arbitary long */ void *argp; /* Arbitary void * */ CRYPTO_EX_new *new_func; CRYPTO_EX_free *free_func; CRYPTO_EX_dup *dup_func; -} CRYPTO_EX_DATA_FUNCS; +} EX_CALLBACK; -DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS) +DECLARE_STACK_OF(EX_CALLBACK) /* - * State for each class; could just be a typedef, but this allows future - * changes. + * The state for each class. This could just be a typedef, but + * a structure allows future changes. */ -typedef struct { - STACK_OF(CRYPTO_EX_DATA_FUNCS) *meth; -} EX_CLASS_ITEM; +typedef struct ex_callbacks_st { + STACK_OF(EX_CALLBACK) *meth; +} EX_CALLBACKS; -static EX_CLASS_ITEM ex_data[CRYPTO_EX_INDEX__COUNT]; +static EX_CALLBACKS ex_data[CRYPTO_EX_INDEX__COUNT]; /* - * Return the EX_CLASS_ITEM from the "ex_data" array that corresponds to + * Return the EX_CALLBACKS from the |ex_data| array that corresponds to * a given class. On success, *holds the lock.* */ -static EX_CLASS_ITEM *def_get_class(int class_index) +static EX_CALLBACKS *get_and_lock(int class_index) { - EX_CLASS_ITEM *ip; + EX_CALLBACKS *ip; if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) { - CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE); + CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE); return NULL; } ip = &ex_data[class_index]; CRYPTO_w_lock(CRYPTO_LOCK_EX_DATA); if (ip->meth == NULL) { - ip->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null(); + ip->meth = sk_EX_CALLBACK_new_null(); /* We push an initial value on the stack because the SSL * "app_data" routines use ex_data index zero. See RT 3710. */ if (ip->meth == NULL - || !sk_CRYPTO_EX_DATA_FUNCS_push(ip->meth, NULL)) { - CRYPTOerr(CRYPTO_F_DEF_GET_CLASS, ERR_R_MALLOC_FAILURE); + || !sk_EX_CALLBACK_push(ip->meth, NULL)) { + CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_MALLOC_FAILURE); CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); return NULL; } @@ -161,7 +166,7 @@ static EX_CLASS_ITEM *def_get_class(int class_index) return ip; } -static void cleanup_cb(CRYPTO_EX_DATA_FUNCS *funcs) +static void cleanup_cb(EX_CALLBACK *funcs) { OPENSSL_free(funcs); } @@ -177,27 +182,71 @@ void CRYPTO_cleanup_all_ex_data(void) int i; for (i = 0; i < CRYPTO_EX_INDEX__COUNT; ++i) { - EX_CLASS_ITEM *ip = &ex_data[i]; + EX_CALLBACKS *ip = &ex_data[i]; - sk_CRYPTO_EX_DATA_FUNCS_pop_free(ip->meth, cleanup_cb); + sk_EX_CALLBACK_pop_free(ip->meth, cleanup_cb); ip->meth = NULL; } } + +/* + * Unregister a new index by replacing the callbacks with no-ops. + * Any in-use instances are leaked. + */ +static void dummy_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, + long argl, void *argp) +{ +} + +static void dummy_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, + long argl, void *argp) +{ +} + +static int dummy_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, + void *from_d, int idx, + long argl, void *argp) +{ + return 0; +} + +int CRYPTO_free_ex_index(int class_index, int idx) +{ + EX_CALLBACKS *ip = get_and_lock(class_index); + EX_CALLBACK *a; + int toret = 0; + + if (ip == NULL) + return 0; + if (idx < 0 || idx >= sk_EX_CALLBACK_num(ip->meth)) + goto err; + a = sk_EX_CALLBACK_value(ip->meth, idx); + if (a == NULL) + goto err; + a->new_func = dummy_new; + a->dup_func = dummy_dup; + a->free_func = dummy_free; + toret = 1; +err: + CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); + return toret; +} + /* - * Inside an existing class, get/register a new index. + * Register a new index. */ int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) { int toret = -1; - CRYPTO_EX_DATA_FUNCS *a; - EX_CLASS_ITEM *ip = def_get_class(class_index); + EX_CALLBACK *a; + EX_CALLBACKS *ip = get_and_lock(class_index); - if (!ip) + if (ip == NULL) return -1; - a = (CRYPTO_EX_DATA_FUNCS *)OPENSSL_malloc(sizeof(*a)); + a = (EX_CALLBACK *)OPENSSL_malloc(sizeof(*a)); if (a == NULL) { CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE); goto err; @@ -208,13 +257,13 @@ int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, a->dup_func = dup_func; a->free_func = free_func; - if (!sk_CRYPTO_EX_DATA_FUNCS_push(ip->meth, NULL)) { + if (!sk_EX_CALLBACK_push(ip->meth, NULL)) { CRYPTOerr(CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX, ERR_R_MALLOC_FAILURE); OPENSSL_free(a); goto err; } - toret = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth) - 1; - (void)sk_CRYPTO_EX_DATA_FUNCS_set(ip->meth, toret, a); + toret = sk_EX_CALLBACK_num(ip->meth) - 1; + (void)sk_EX_CALLBACK_set(ip->meth, toret, a); err: CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); @@ -224,7 +273,7 @@ int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, /* * Initialise a new CRYPTO_EX_DATA for use in a particular class - including * calling new() callbacks for each index in the class used by this variable - * Thread-safe by copying a class's array of "CRYPTO_EX_DATA_FUNCS" entries + * Thread-safe by copying a class's array of "EX_CALLBACK" entries * in the lock, then using them outside the lock. Note this only applies * to the global "ex_data" state (ie. class definitions), not 'ad' itself. */ @@ -232,16 +281,16 @@ int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) { int mx, i; void *ptr; - CRYPTO_EX_DATA_FUNCS **storage = NULL; - CRYPTO_EX_DATA_FUNCS *stack[10]; - EX_CLASS_ITEM *ip = def_get_class(class_index); + EX_CALLBACK **storage = NULL; + EX_CALLBACK *stack[10]; + EX_CALLBACKS *ip = get_and_lock(class_index); - if (!ip) + if (ip == NULL) return 0; ad->sk = NULL; - mx = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth); + mx = sk_EX_CALLBACK_num(ip->meth); if (mx > 0) { if (mx < (int)OSSL_NELEM(stack)) storage = stack; @@ -249,7 +298,7 @@ int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) storage = OPENSSL_malloc(sizeof(*storage) * mx); if (storage != NULL) for (i = 0; i < mx; i++) - storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(ip->meth, i); + storage[i] = sk_EX_CALLBACK_value(ip->meth, i); } CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); @@ -278,17 +327,17 @@ int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, { int mx, j, i; char *ptr; - CRYPTO_EX_DATA_FUNCS *stack[10]; - CRYPTO_EX_DATA_FUNCS **storage = NULL; - EX_CLASS_ITEM *ip; + EX_CALLBACK *stack[10]; + EX_CALLBACK **storage = NULL; + EX_CALLBACKS *ip; if (from->sk == NULL) /* Nothing to copy over */ return 1; - if ((ip = def_get_class(class_index)) == NULL) + if ((ip = get_and_lock(class_index)) == NULL) return 0; - mx = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth); + mx = sk_EX_CALLBACK_num(ip->meth); j = sk_void_num(from->sk); if (j < mx) mx = j; @@ -299,7 +348,7 @@ int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, storage = OPENSSL_malloc(sizeof(*storage) * mx); if (storage != NULL) for (i = 0; i < mx; i++) - storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(ip->meth, i); + storage[i] = sk_EX_CALLBACK_value(ip->meth, i); } CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); @@ -328,15 +377,15 @@ int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) { int mx, i; - EX_CLASS_ITEM *ip; + EX_CALLBACKS *ip; void *ptr; - CRYPTO_EX_DATA_FUNCS *stack[10]; - CRYPTO_EX_DATA_FUNCS **storage = NULL; + EX_CALLBACK *stack[10]; + EX_CALLBACK **storage = NULL; - if ((ip = def_get_class(class_index)) == NULL) + if ((ip = get_and_lock(class_index)) == NULL) return; - mx = sk_CRYPTO_EX_DATA_FUNCS_num(ip->meth); + mx = sk_EX_CALLBACK_num(ip->meth); if (mx > 0) { if (mx < (int)OSSL_NELEM(stack)) storage = stack; @@ -344,7 +393,7 @@ void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad) storage = OPENSSL_malloc(sizeof(*storage) * mx); if (storage != NULL) for (i = 0; i < mx; i++) - storage[i] = sk_CRYPTO_EX_DATA_FUNCS_value(ip->meth, i); + storage[i] = sk_EX_CALLBACK_value(ip->meth, i); } CRYPTO_w_unlock(CRYPTO_LOCK_EX_DATA); diff --git a/crypto/rsa/rsa_lib.c b/crypto/rsa/rsa_lib.c index 594722bda8..c9249ef7f0 100644 --- a/crypto/rsa/rsa_lib.c +++ b/crypto/rsa/rsa_lib.c @@ -234,13 +234,6 @@ int RSA_up_ref(RSA *r) return ((i > 1) ? 1 : 0); } -int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, - new_func, dup_func, free_func); -} - int RSA_set_ex_data(RSA *r, int idx, void *arg) { return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); diff --git a/crypto/store/str_lib.c b/crypto/store/str_lib.c index c2e6b8e8d1..d5ecdbdbf4 100644 --- a/crypto/store/str_lib.c +++ b/crypto/store/str_lib.c @@ -187,13 +187,6 @@ int STORE_ctrl(STORE *store, int cmd, long i, void *p, void (*f) (void)) return 0; } -int STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_STORE, argl, argp, - new_func, dup_func, free_func); -} - int STORE_set_ex_data(STORE *r, int idx, void *arg) { return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); diff --git a/crypto/ui/ui_lib.c b/crypto/ui/ui_lib.c index 9440a857c4..d2ac28ba7f 100644 --- a/crypto/ui/ui_lib.c +++ b/crypto/ui/ui_lib.c @@ -536,13 +536,6 @@ int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void)) return -1; } -int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp, - new_func, dup_func, free_func); -} - int UI_set_ex_data(UI *r, int idx, void *arg) { return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); diff --git a/crypto/x509/x509_vfy.c b/crypto/x509/x509_vfy.c index 0a47d4da20..3156e04225 100644 --- a/crypto/x509/x509_vfy.c +++ b/crypto/x509/x509_vfy.c @@ -2110,19 +2110,6 @@ X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer, return NULL; } -int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, - CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, - CRYPTO_EX_free *free_func) -{ - /* - * This function is (usually) called only once, by - * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). - */ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp, - new_func, dup_func, free_func); -} - int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) { return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); diff --git a/crypto/x509/x_x509.c b/crypto/x509/x_x509.c index cab17ddee6..47333217e7 100644 --- a/crypto/x509/x_x509.c +++ b/crypto/x509/x_x509.c @@ -142,13 +142,6 @@ IMPLEMENT_ASN1_FUNCTIONS(X509) IMPLEMENT_ASN1_DUP_FUNCTION(X509) -int X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) -{ - return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509, argl, argp, - new_func, dup_func, free_func); -} - int X509_set_ex_data(X509 *r, int idx, void *arg) { return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); |