| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/ex_data.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 |
| 22 | #include <openssl/crypto.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/thread.h> |
| 26 |
| 27 | #include "internal.h" |
| 28 |
| 29 |
| 30 | DEFINE_STACK_OF(CRYPTO_EX_DATA_FUNCS) |
| 31 |
| 32 | struct crypto_ex_data_func_st { |
| 33 | long argl; // Arbitary long |
| 34 | void *argp; // Arbitary void pointer |
| 35 | CRYPTO_EX_free *free_func; |
| 36 | // next points to the next |CRYPTO_EX_DATA_FUNCS| or NULL if this is the last |
| 37 | // one. It may only be read if synchronized with a read from |num_funcs|. |
| 38 | CRYPTO_EX_DATA_FUNCS *next; |
| 39 | }; |
| 40 |
| 41 | int CRYPTO_get_ex_new_index_ex(CRYPTO_EX_DATA_CLASS *ex_data_class, long argl, |
| 42 | void *argp, CRYPTO_EX_free *free_func) { |
| 43 | CRYPTO_EX_DATA_FUNCS *funcs = reinterpret_cast<CRYPTO_EX_DATA_FUNCS *>( |
| 44 | OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS))); |
| 45 | if (funcs == NULL) { |
| 46 | return -1; |
| 47 | } |
| 48 |
| 49 | funcs->argl = argl; |
| 50 | funcs->argp = argp; |
| 51 | funcs->free_func = free_func; |
| 52 | funcs->next = NULL; |
| 53 |
| 54 | CRYPTO_MUTEX_lock_write(&ex_data_class->lock); |
| 55 |
| 56 | uint32_t num_funcs = CRYPTO_atomic_load_u32(&ex_data_class->num_funcs); |
| 57 | // The index must fit in |int|. |
| 58 | if (num_funcs > (size_t)(INT_MAX - ex_data_class->num_reserved)) { |
| 59 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 60 | CRYPTO_MUTEX_unlock_write(&ex_data_class->lock); |
| 61 | return -1; |
| 62 | } |
| 63 |
| 64 | // Append |funcs| to the linked list. |
| 65 | if (ex_data_class->last == NULL) { |
| 66 | assert(num_funcs == 0); |
| 67 | ex_data_class->funcs = funcs; |
| 68 | ex_data_class->last = funcs; |
| 69 | } else { |
| 70 | ex_data_class->last->next = funcs; |
| 71 | ex_data_class->last = funcs; |
| 72 | } |
| 73 |
| 74 | CRYPTO_atomic_store_u32(&ex_data_class->num_funcs, num_funcs + 1); |
| 75 | CRYPTO_MUTEX_unlock_write(&ex_data_class->lock); |
| 76 | return (int)num_funcs + ex_data_class->num_reserved; |
| 77 | } |
| 78 |
| 79 | int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) { |
| 80 | if (index < 0) { |
| 81 | // A caller that can accidentally pass in an invalid index into this |
| 82 | // function will hit an memory error if |index| happened to be valid, and |
| 83 | // expected |val| to be of a different type. |
| 84 | abort(); |
| 85 | } |
| 86 |
| 87 | if (ad->sk == NULL) { |
| 88 | ad->sk = sk_void_new_null(); |
| 89 | if (ad->sk == NULL) { |
| 90 | return 0; |
| 91 | } |
| 92 | } |
| 93 |
| 94 | // Add NULL values until the stack is long enough. |
| 95 | for (size_t i = sk_void_num(ad->sk); i <= (size_t)index; i++) { |
| 96 | if (!sk_void_push(ad->sk, NULL)) { |
| 97 | return 0; |
| 98 | } |
| 99 | } |
| 100 |
| 101 | sk_void_set(ad->sk, (size_t)index, val); |
| 102 | return 1; |
| 103 | } |
| 104 |
| 105 | void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) { |
| 106 | if (ad->sk == NULL || idx < 0 || (size_t)idx >= sk_void_num(ad->sk)) { |
| 107 | return NULL; |
| 108 | } |
| 109 | return sk_void_value(ad->sk, idx); |
| 110 | } |
| 111 |
| 112 | void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad) { ad->sk = NULL; } |
| 113 |
| 114 | void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class, void *obj, |
| 115 | CRYPTO_EX_DATA *ad) { |
| 116 | if (ad->sk == NULL) { |
| 117 | // Nothing to do. |
| 118 | return; |
| 119 | } |
| 120 |
| 121 | uint32_t num_funcs = CRYPTO_atomic_load_u32(&ex_data_class->num_funcs); |
| 122 | // |CRYPTO_get_ex_new_index_ex| will not allocate indices beyond |INT_MAX|. |
| 123 | assert(num_funcs <= (size_t)(INT_MAX - ex_data_class->num_reserved)); |
| 124 |
| 125 | // Defer dereferencing |ex_data_class->funcs| and |funcs->next|. It must come |
| 126 | // after the |num_funcs| comparison to be correctly synchronized. |
| 127 | CRYPTO_EX_DATA_FUNCS *const *funcs = &ex_data_class->funcs; |
| 128 | for (uint32_t i = 0; i < num_funcs; i++) { |
| 129 | if ((*funcs)->free_func != NULL) { |
| 130 | int index = (int)i + ex_data_class->num_reserved; |
| 131 | void *ptr = CRYPTO_get_ex_data(ad, index); |
| 132 | (*funcs)->free_func(obj, ptr, ad, index, (*funcs)->argl, (*funcs)->argp); |
| 133 | } |
| 134 | funcs = &(*funcs)->next; |
| 135 | } |
| 136 |
| 137 | sk_void_free(ad->sk); |
| 138 | ad->sk = NULL; |
| 139 | } |
| 140 |
| 141 | void CRYPTO_cleanup_all_ex_data(void) {} |
| 142 |