| 1 |
/* |
| 2 |
* Copyright 1998-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include <openssl/err.h> |
| 15 |
#include <openssl/lhash.h> |
| 16 |
#include <openssl/objects.h> |
| 17 |
#include <openssl/safestack.h> |
| 18 |
#include <openssl/e_os2.h> |
| 19 |
#include "internal/thread_once.h" |
| 20 |
#include "crypto/lhash.h" |
| 21 |
#include "obj_local.h" |
| 22 |
#include "internal/e_os.h" |
| 23 |
|
| 24 |
/* |
| 25 |
* I use the ex_data stuff to manage the identifiers for the obj_name_types |
| 26 |
* that applications may define. I only really use the free function field. |
| 27 |
*/ |
| 28 |
static LHASH_OF(OBJ_NAME) *names_lh = NULL; |
| 29 |
static int names_type_num = OBJ_NAME_TYPE_NUM; |
| 30 |
static CRYPTO_RWLOCK *obj_lock = NULL; |
| 31 |
|
| 32 |
struct name_funcs_st { |
| 33 |
unsigned long (*hash_func)(const char *name); |
| 34 |
int (*cmp_func)(const char *a, const char *b); |
| 35 |
void (*free_func)(const char *, int, const char *); |
| 36 |
}; |
| 37 |
|
| 38 |
static STACK_OF(NAME_FUNCS) *name_funcs_stack; |
| 39 |
|
| 40 |
/* |
| 41 |
* The LHASH callbacks now use the raw "void *" prototypes and do |
| 42 |
* per-variable casting in the functions. This prevents function pointer |
| 43 |
* casting without the need for macro-generated wrapper functions. |
| 44 |
*/ |
| 45 |
|
| 46 |
static unsigned long obj_name_hash(const OBJ_NAME *a); |
| 47 |
static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b); |
| 48 |
|
| 49 |
static CRYPTO_ONCE init = CRYPTO_ONCE_STATIC_INIT; |
| 50 |
DEFINE_RUN_ONCE_STATIC(o_names_init) |
| 51 |
{ |
| 52 |
names_lh = NULL; |
| 53 |
obj_lock = CRYPTO_THREAD_lock_new(); |
| 54 |
if (obj_lock != NULL) |
| 55 |
names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp); |
| 56 |
if (names_lh == NULL) { |
| 57 |
CRYPTO_THREAD_lock_free(obj_lock); |
| 58 |
obj_lock = NULL; |
| 59 |
} |
| 60 |
return names_lh != NULL && obj_lock != NULL; |
| 61 |
} |
| 62 |
|
| 63 |
int OBJ_NAME_init(void) |
| 64 |
{ |
| 65 |
return RUN_ONCE(&init, o_names_init); |
| 66 |
} |
| 67 |
|
| 68 |
int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), |
| 69 |
int (*cmp_func)(const char *, const char *), |
| 70 |
void (*free_func)(const char *, int, const char *)) |
| 71 |
{ |
| 72 |
int ret = 0, i, push; |
| 73 |
NAME_FUNCS *name_funcs; |
| 74 |
|
| 75 |
if (!OBJ_NAME_init()) |
| 76 |
return 0; |
| 77 |
|
| 78 |
if (!CRYPTO_THREAD_write_lock(obj_lock)) |
| 79 |
return 0; |
| 80 |
|
| 81 |
if (name_funcs_stack == NULL) |
| 82 |
name_funcs_stack = sk_NAME_FUNCS_new_null(); |
| 83 |
if (name_funcs_stack == NULL) { |
| 84 |
/* ERROR */ |
| 85 |
goto out; |
| 86 |
} |
| 87 |
ret = names_type_num; |
| 88 |
names_type_num++; |
| 89 |
for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) { |
| 90 |
name_funcs = OPENSSL_zalloc(sizeof(*name_funcs)); |
| 91 |
if (name_funcs == NULL) { |
| 92 |
ret = 0; |
| 93 |
goto out; |
| 94 |
} |
| 95 |
name_funcs->hash_func = ossl_lh_strcasehash; |
| 96 |
name_funcs->cmp_func = OPENSSL_strcasecmp; |
| 97 |
push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs); |
| 98 |
|
| 99 |
if (!push) { |
| 100 |
ERR_raise(ERR_LIB_OBJ, ERR_R_CRYPTO_LIB); |
| 101 |
OPENSSL_free(name_funcs); |
| 102 |
ret = 0; |
| 103 |
goto out; |
| 104 |
} |
| 105 |
} |
| 106 |
name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret); |
| 107 |
if (hash_func != NULL) |
| 108 |
name_funcs->hash_func = hash_func; |
| 109 |
if (cmp_func != NULL) |
| 110 |
name_funcs->cmp_func = cmp_func; |
| 111 |
if (free_func != NULL) |
| 112 |
name_funcs->free_func = free_func; |
| 113 |
|
| 114 |
out: |
| 115 |
CRYPTO_THREAD_unlock(obj_lock); |
| 116 |
return ret; |
| 117 |
} |
| 118 |
|
| 119 |
static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b) |
| 120 |
{ |
| 121 |
int ret; |
| 122 |
|
| 123 |
ret = a->type - b->type; |
| 124 |
if (ret == 0) { |
| 125 |
if ((name_funcs_stack != NULL) |
| 126 |
&& (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) { |
| 127 |
ret = sk_NAME_FUNCS_value(name_funcs_stack, |
| 128 |
a->type) |
| 129 |
->cmp_func(a->name, b->name); |
| 130 |
} else |
| 131 |
ret = OPENSSL_strcasecmp(a->name, b->name); |
| 132 |
} |
| 133 |
return ret; |
| 134 |
} |
| 135 |
|
| 136 |
static unsigned long obj_name_hash(const OBJ_NAME *a) |
| 137 |
{ |
| 138 |
unsigned long ret; |
| 139 |
|
| 140 |
if ((name_funcs_stack != NULL) |
| 141 |
&& (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) { |
| 142 |
ret = sk_NAME_FUNCS_value(name_funcs_stack, |
| 143 |
a->type) |
| 144 |
->hash_func(a->name); |
| 145 |
} else { |
| 146 |
ret = ossl_lh_strcasehash(a->name); |
| 147 |
} |
| 148 |
ret ^= a->type; |
| 149 |
return ret; |
| 150 |
} |
| 151 |
|
| 152 |
const char *OBJ_NAME_get(const char *name, int type) |
| 153 |
{ |
| 154 |
OBJ_NAME on, *ret; |
| 155 |
int num = 0, alias; |
| 156 |
const char *value = NULL; |
| 157 |
|
| 158 |
if (name == NULL) |
| 159 |
return NULL; |
| 160 |
if (!OBJ_NAME_init()) |
| 161 |
return NULL; |
| 162 |
if (!CRYPTO_THREAD_read_lock(obj_lock)) |
| 163 |
return NULL; |
| 164 |
|
| 165 |
alias = type & OBJ_NAME_ALIAS; |
| 166 |
type &= ~OBJ_NAME_ALIAS; |
| 167 |
|
| 168 |
on.name = name; |
| 169 |
on.type = type; |
| 170 |
|
| 171 |
for (;;) { |
| 172 |
ret = lh_OBJ_NAME_retrieve(names_lh, &on); |
| 173 |
if (ret == NULL) |
| 174 |
break; |
| 175 |
if ((ret->alias) && !alias) { |
| 176 |
if (++num > 10) |
| 177 |
break; |
| 178 |
on.name = ret->data; |
| 179 |
} else { |
| 180 |
value = ret->data; |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
CRYPTO_THREAD_unlock(obj_lock); |
| 186 |
return value; |
| 187 |
} |
| 188 |
|
| 189 |
int OBJ_NAME_add(const char *name, int type, const char *data) |
| 190 |
{ |
| 191 |
OBJ_NAME *onp, *ret; |
| 192 |
int alias, ok = 0; |
| 193 |
|
| 194 |
if (!OBJ_NAME_init()) |
| 195 |
return 0; |
| 196 |
|
| 197 |
alias = type & OBJ_NAME_ALIAS; |
| 198 |
type &= ~OBJ_NAME_ALIAS; |
| 199 |
|
| 200 |
onp = OPENSSL_malloc(sizeof(*onp)); |
| 201 |
if (onp == NULL) |
| 202 |
return 0; |
| 203 |
|
| 204 |
onp->name = name; |
| 205 |
onp->alias = alias; |
| 206 |
onp->type = type; |
| 207 |
onp->data = data; |
| 208 |
|
| 209 |
if (!CRYPTO_THREAD_write_lock(obj_lock)) { |
| 210 |
OPENSSL_free(onp); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
ret = lh_OBJ_NAME_insert(names_lh, onp); |
| 215 |
if (ret != NULL) { |
| 216 |
/* free things */ |
| 217 |
if ((name_funcs_stack != NULL) |
| 218 |
&& (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) { |
| 219 |
/* |
| 220 |
* XXX: I'm not sure I understand why the free function should |
| 221 |
* get three arguments... -- Richard Levitte |
| 222 |
*/ |
| 223 |
sk_NAME_FUNCS_value(name_funcs_stack, |
| 224 |
ret->type) |
| 225 |
->free_func(ret->name, ret->type, |
| 226 |
ret->data); |
| 227 |
} |
| 228 |
OPENSSL_free(ret); |
| 229 |
} else { |
| 230 |
if (lh_OBJ_NAME_error(names_lh)) { |
| 231 |
/* ERROR */ |
| 232 |
OPENSSL_free(onp); |
| 233 |
goto unlock; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
ok = 1; |
| 238 |
|
| 239 |
unlock: |
| 240 |
CRYPTO_THREAD_unlock(obj_lock); |
| 241 |
return ok; |
| 242 |
} |
| 243 |
|
| 244 |
int OBJ_NAME_remove(const char *name, int type) |
| 245 |
{ |
| 246 |
OBJ_NAME on, *ret; |
| 247 |
int ok = 0; |
| 248 |
|
| 249 |
if (!OBJ_NAME_init()) |
| 250 |
return 0; |
| 251 |
|
| 252 |
if (!CRYPTO_THREAD_write_lock(obj_lock)) |
| 253 |
return 0; |
| 254 |
|
| 255 |
type &= ~OBJ_NAME_ALIAS; |
| 256 |
on.name = name; |
| 257 |
on.type = type; |
| 258 |
ret = lh_OBJ_NAME_delete(names_lh, &on); |
| 259 |
if (ret != NULL) { |
| 260 |
/* free things */ |
| 261 |
if ((name_funcs_stack != NULL) |
| 262 |
&& (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) { |
| 263 |
/* |
| 264 |
* XXX: I'm not sure I understand why the free function should |
| 265 |
* get three arguments... -- Richard Levitte |
| 266 |
*/ |
| 267 |
sk_NAME_FUNCS_value(name_funcs_stack, |
| 268 |
ret->type) |
| 269 |
->free_func(ret->name, ret->type, |
| 270 |
ret->data); |
| 271 |
} |
| 272 |
OPENSSL_free(ret); |
| 273 |
ok = 1; |
| 274 |
} |
| 275 |
|
| 276 |
CRYPTO_THREAD_unlock(obj_lock); |
| 277 |
return ok; |
| 278 |
} |
| 279 |
|
| 280 |
typedef struct { |
| 281 |
int type; |
| 282 |
void (*fn)(const OBJ_NAME *, void *arg); |
| 283 |
void *arg; |
| 284 |
} OBJ_DOALL; |
| 285 |
|
| 286 |
static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d) |
| 287 |
{ |
| 288 |
if (name->type == d->type) |
| 289 |
d->fn(name, d->arg); |
| 290 |
} |
| 291 |
|
| 292 |
IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL); |
| 293 |
|
| 294 |
void OBJ_NAME_do_all(int type, void (*fn)(const OBJ_NAME *, void *arg), |
| 295 |
void *arg) |
| 296 |
{ |
| 297 |
OBJ_DOALL d; |
| 298 |
|
| 299 |
d.type = type; |
| 300 |
d.fn = fn; |
| 301 |
d.arg = arg; |
| 302 |
|
| 303 |
lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d); |
| 304 |
} |
| 305 |
|
| 306 |
struct doall_sorted { |
| 307 |
int type; |
| 308 |
int n; |
| 309 |
const OBJ_NAME **names; |
| 310 |
}; |
| 311 |
|
| 312 |
static void do_all_sorted_fn(const OBJ_NAME *name, void *d_) |
| 313 |
{ |
| 314 |
struct doall_sorted *d = d_; |
| 315 |
|
| 316 |
if (name->type != d->type) |
| 317 |
return; |
| 318 |
|
| 319 |
d->names[d->n++] = name; |
| 320 |
} |
| 321 |
|
| 322 |
static int do_all_sorted_cmp(const void *n1_, const void *n2_) |
| 323 |
{ |
| 324 |
const OBJ_NAME *const *n1 = n1_; |
| 325 |
const OBJ_NAME *const *n2 = n2_; |
| 326 |
|
| 327 |
return strcmp((*n1)->name, (*n2)->name); |
| 328 |
} |
| 329 |
|
| 330 |
void OBJ_NAME_do_all_sorted(int type, |
| 331 |
void (*fn)(const OBJ_NAME *, void *arg), |
| 332 |
void *arg) |
| 333 |
{ |
| 334 |
struct doall_sorted d; |
| 335 |
int n; |
| 336 |
|
| 337 |
d.type = type; |
| 338 |
d.names = OPENSSL_malloc_array(lh_OBJ_NAME_num_items(names_lh), sizeof(*d.names)); |
| 339 |
/* Really should return an error if !d.names...but its a void function! */ |
| 340 |
if (d.names != NULL) { |
| 341 |
d.n = 0; |
| 342 |
OBJ_NAME_do_all(type, do_all_sorted_fn, &d); |
| 343 |
|
| 344 |
qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp); |
| 345 |
|
| 346 |
for (n = 0; n < d.n; ++n) |
| 347 |
fn(d.names[n], arg); |
| 348 |
|
| 349 |
OPENSSL_free((void *)d.names); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
static int free_type; |
| 354 |
|
| 355 |
static void names_lh_free_doall(OBJ_NAME *onp) |
| 356 |
{ |
| 357 |
if (onp == NULL) |
| 358 |
return; |
| 359 |
|
| 360 |
if (free_type < 0 || free_type == onp->type) |
| 361 |
OBJ_NAME_remove(onp->name, onp->type); |
| 362 |
} |
| 363 |
|
| 364 |
static void name_funcs_free(NAME_FUNCS *ptr) |
| 365 |
{ |
| 366 |
OPENSSL_free(ptr); |
| 367 |
} |
| 368 |
|
| 369 |
void OBJ_NAME_cleanup(int type) |
| 370 |
{ |
| 371 |
unsigned long down_load; |
| 372 |
|
| 373 |
if (names_lh == NULL) |
| 374 |
return; |
| 375 |
|
| 376 |
free_type = type; |
| 377 |
down_load = lh_OBJ_NAME_get_down_load(names_lh); |
| 378 |
lh_OBJ_NAME_set_down_load(names_lh, 0); |
| 379 |
|
| 380 |
lh_OBJ_NAME_doall(names_lh, names_lh_free_doall); |
| 381 |
if (type < 0) { |
| 382 |
lh_OBJ_NAME_free(names_lh); |
| 383 |
sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free); |
| 384 |
CRYPTO_THREAD_lock_free(obj_lock); |
| 385 |
names_lh = NULL; |
| 386 |
name_funcs_stack = NULL; |
| 387 |
obj_lock = NULL; |
| 388 |
} else |
| 389 |
lh_OBJ_NAME_set_down_load(names_lh, down_load); |
| 390 |
} |