| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/ex_data.h> |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include <openssl/crypto.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
#include <openssl/mem.h> |
| 15 |
#include <openssl/stack.h> |
| 16 |
#include <openssl/thread.h> |
| 17 |
|
| 18 |
#include "internal.h" |
| 19 |
|
| 20 |
|
| 21 |
DEFINE_STACK_OF(CRYPTO_EX_DATA_FUNCS) |
| 22 |
|
| 23 |
struct crypto_ex_data_func_st { |
| 24 |
long argl; // Arbitary long |
| 25 |
void *argp; // Arbitary void pointer |
| 26 |
CRYPTO_EX_free *free_func; |
| 27 |
}; |
| 28 |
|
| 29 |
int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class, int *out_index, |
| 30 |
long argl, void *argp, CRYPTO_EX_free *free_func) { |
| 31 |
CRYPTO_EX_DATA_FUNCS *funcs; |
| 32 |
int ret = 0; |
| 33 |
|
| 34 |
funcs = OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS)); |
| 35 |
if (funcs == NULL) { |
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
funcs->argl = argl; |
| 40 |
funcs->argp = argp; |
| 41 |
funcs->free_func = free_func; |
| 42 |
|
| 43 |
CRYPTO_STATIC_MUTEX_lock_write(&ex_data_class->lock); |
| 44 |
|
| 45 |
if (ex_data_class->meth == NULL) { |
| 46 |
ex_data_class->meth = sk_CRYPTO_EX_DATA_FUNCS_new_null(); |
| 47 |
} |
| 48 |
|
| 49 |
if (ex_data_class->meth == NULL) { |
| 50 |
goto err; |
| 51 |
} |
| 52 |
|
| 53 |
// The index must fit in |int|. |
| 54 |
if (sk_CRYPTO_EX_DATA_FUNCS_num(ex_data_class->meth) > |
| 55 |
(size_t)(INT_MAX - ex_data_class->num_reserved)) { |
| 56 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 57 |
goto err; |
| 58 |
} |
| 59 |
|
| 60 |
if (!sk_CRYPTO_EX_DATA_FUNCS_push(ex_data_class->meth, funcs)) { |
| 61 |
goto err; |
| 62 |
} |
| 63 |
funcs = NULL; // |sk_CRYPTO_EX_DATA_FUNCS_push| takes ownership. |
| 64 |
|
| 65 |
*out_index = (int)sk_CRYPTO_EX_DATA_FUNCS_num(ex_data_class->meth) - 1 + |
| 66 |
ex_data_class->num_reserved; |
| 67 |
ret = 1; |
| 68 |
|
| 69 |
err: |
| 70 |
CRYPTO_STATIC_MUTEX_unlock_write(&ex_data_class->lock); |
| 71 |
OPENSSL_free(funcs); |
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) { |
| 76 |
if (index < 0) { |
| 77 |
// A caller that can accidentally pass in an invalid index into this |
| 78 |
// function will hit an memory error if |index| happened to be valid, and |
| 79 |
// expected |val| to be of a different type. |
| 80 |
abort(); |
| 81 |
} |
| 82 |
|
| 83 |
if (ad->sk == NULL) { |
| 84 |
ad->sk = sk_void_new_null(); |
| 85 |
if (ad->sk == NULL) { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Add NULL values until the stack is long enough. |
| 91 |
for (size_t i = sk_void_num(ad->sk); i <= (size_t)index; i++) { |
| 92 |
if (!sk_void_push(ad->sk, NULL)) { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
sk_void_set(ad->sk, (size_t)index, val); |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) { |
| 102 |
if (ad->sk == NULL || idx < 0 || (size_t)idx >= sk_void_num(ad->sk)) { |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
return sk_void_value(ad->sk, idx); |
| 106 |
} |
| 107 |
|
| 108 |
// get_func_pointers takes a copy of the CRYPTO_EX_DATA_FUNCS pointers, if any, |
| 109 |
// for the given class. If there are some pointers, it sets |*out| to point to |
| 110 |
// a fresh stack of them. Otherwise it sets |*out| to NULL. It returns one on |
| 111 |
// success or zero on error. |
| 112 |
static int get_func_pointers(STACK_OF(CRYPTO_EX_DATA_FUNCS) **out, |
| 113 |
CRYPTO_EX_DATA_CLASS *ex_data_class) { |
| 114 |
size_t n; |
| 115 |
|
| 116 |
*out = NULL; |
| 117 |
|
| 118 |
// CRYPTO_EX_DATA_FUNCS structures are static once set, so we can take a |
| 119 |
// shallow copy of the list under lock and then use the structures without |
| 120 |
// the lock held. |
| 121 |
CRYPTO_STATIC_MUTEX_lock_read(&ex_data_class->lock); |
| 122 |
n = sk_CRYPTO_EX_DATA_FUNCS_num(ex_data_class->meth); |
| 123 |
if (n > 0) { |
| 124 |
*out = sk_CRYPTO_EX_DATA_FUNCS_dup(ex_data_class->meth); |
| 125 |
} |
| 126 |
CRYPTO_STATIC_MUTEX_unlock_read(&ex_data_class->lock); |
| 127 |
|
| 128 |
if (n > 0 && *out == NULL) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
return 1; |
| 133 |
} |
| 134 |
|
| 135 |
void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad) { |
| 136 |
ad->sk = NULL; |
| 137 |
} |
| 138 |
|
| 139 |
void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class, void *obj, |
| 140 |
CRYPTO_EX_DATA *ad) { |
| 141 |
if (ad->sk == NULL) { |
| 142 |
// Nothing to do. |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
STACK_OF(CRYPTO_EX_DATA_FUNCS) *func_pointers; |
| 147 |
if (!get_func_pointers(&func_pointers, ex_data_class)) { |
| 148 |
// TODO(davidben): This leaks memory on malloc error. |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// |CRYPTO_get_ex_new_index| will not allocate indices beyond |INT_MAX|. |
| 153 |
assert(sk_CRYPTO_EX_DATA_FUNCS_num(func_pointers) <= |
| 154 |
(size_t)(INT_MAX - ex_data_class->num_reserved)); |
| 155 |
for (int i = 0; i < (int)sk_CRYPTO_EX_DATA_FUNCS_num(func_pointers); i++) { |
| 156 |
CRYPTO_EX_DATA_FUNCS *func_pointer = |
| 157 |
sk_CRYPTO_EX_DATA_FUNCS_value(func_pointers, i); |
| 158 |
if (func_pointer->free_func) { |
| 159 |
void *ptr = CRYPTO_get_ex_data(ad, i + ex_data_class->num_reserved); |
| 160 |
func_pointer->free_func(obj, ptr, ad, i + ex_data_class->num_reserved, |
| 161 |
func_pointer->argl, func_pointer->argp); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
sk_CRYPTO_EX_DATA_FUNCS_free(func_pointers); |
| 166 |
|
| 167 |
sk_void_free(ad->sk); |
| 168 |
ad->sk = NULL; |
| 169 |
} |
| 170 |
|
| 171 |
void CRYPTO_cleanup_all_ex_data(void) {} |