2018-11-16 09:44:30 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2018-11-16 09:44:30 +08:00
|
|
|
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#include <openssl/lhash.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/lhash.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "property_local.h"
|
2022-03-14 16:13:12 +08:00
|
|
|
#include "crypto/context.h"
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Property strings are a consolidation of all strings seen by the property
|
|
|
|
* subsystem. There are two name spaces to keep property names separate from
|
|
|
|
* property values (numeric values are not expected to be cached however).
|
|
|
|
* They allow a rapid conversion from a string to a unique index and any
|
|
|
|
* subsequent string comparison can be done via an integer compare.
|
|
|
|
*
|
|
|
|
* This implementation uses OpenSSL's standard hash table. There are more
|
|
|
|
* space and time efficient algorithms if this becomes a bottleneck.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *s;
|
|
|
|
OSSL_PROPERTY_IDX idx;
|
|
|
|
char body[1];
|
|
|
|
} PROPERTY_STRING;
|
|
|
|
|
2022-03-22 19:52:27 +08:00
|
|
|
DEFINE_LHASH_OF_EX(PROPERTY_STRING);
|
2018-11-16 09:44:30 +08:00
|
|
|
typedef LHASH_OF(PROPERTY_STRING) PROP_TABLE;
|
|
|
|
|
2019-05-01 18:02:43 +08:00
|
|
|
typedef struct {
|
2021-06-23 12:18:07 +08:00
|
|
|
CRYPTO_RWLOCK *lock;
|
2019-05-01 18:02:43 +08:00
|
|
|
PROP_TABLE *prop_names;
|
|
|
|
PROP_TABLE *prop_values;
|
|
|
|
OSSL_PROPERTY_IDX prop_name_idx;
|
|
|
|
OSSL_PROPERTY_IDX prop_value_idx;
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifndef OPENSSL_SMALL_FOOTPRINT
|
2021-12-21 08:44:31 +08:00
|
|
|
STACK_OF(OPENSSL_CSTRING) *prop_namelist;
|
|
|
|
STACK_OF(OPENSSL_CSTRING) *prop_valuelist;
|
2022-01-01 09:43:31 +08:00
|
|
|
#endif
|
2019-05-01 18:02:43 +08:00
|
|
|
} PROPERTY_STRING_DATA;
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
static unsigned long property_hash(const PROPERTY_STRING *a)
|
|
|
|
{
|
|
|
|
return OPENSSL_LH_strhash(a->s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int property_cmp(const PROPERTY_STRING *a, const PROPERTY_STRING *b)
|
|
|
|
{
|
|
|
|
return strcmp(a->s, b->s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void property_free(PROPERTY_STRING *ps)
|
|
|
|
{
|
|
|
|
OPENSSL_free(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void property_table_free(PROP_TABLE **pt)
|
|
|
|
{
|
|
|
|
PROP_TABLE *t = *pt;
|
|
|
|
|
|
|
|
if (t != NULL) {
|
|
|
|
lh_PROPERTY_STRING_doall(t, &property_free);
|
|
|
|
lh_PROPERTY_STRING_free(t);
|
|
|
|
*pt = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 16:13:12 +08:00
|
|
|
void ossl_property_string_data_free(void *vpropdata)
|
2019-05-01 18:02:43 +08:00
|
|
|
{
|
|
|
|
PROPERTY_STRING_DATA *propdata = vpropdata;
|
|
|
|
|
|
|
|
if (propdata == NULL)
|
|
|
|
return;
|
|
|
|
|
2021-06-23 12:18:07 +08:00
|
|
|
CRYPTO_THREAD_lock_free(propdata->lock);
|
2019-05-01 18:02:43 +08:00
|
|
|
property_table_free(&propdata->prop_names);
|
|
|
|
property_table_free(&propdata->prop_values);
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifndef OPENSSL_SMALL_FOOTPRINT
|
2021-12-21 08:44:31 +08:00
|
|
|
sk_OPENSSL_CSTRING_free(propdata->prop_namelist);
|
|
|
|
sk_OPENSSL_CSTRING_free(propdata->prop_valuelist);
|
|
|
|
propdata->prop_namelist = propdata->prop_valuelist = NULL;
|
2022-01-01 09:43:31 +08:00
|
|
|
#endif
|
2019-05-01 18:02:43 +08:00
|
|
|
propdata->prop_name_idx = propdata->prop_value_idx = 0;
|
|
|
|
|
|
|
|
OPENSSL_free(propdata);
|
|
|
|
}
|
|
|
|
|
2022-03-14 16:13:12 +08:00
|
|
|
void *ossl_property_string_data_new(OSSL_LIB_CTX *ctx) {
|
2019-05-01 18:02:43 +08:00
|
|
|
PROPERTY_STRING_DATA *propdata = OPENSSL_zalloc(sizeof(*propdata));
|
|
|
|
|
|
|
|
if (propdata == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2021-06-23 12:18:07 +08:00
|
|
|
propdata->lock = CRYPTO_THREAD_lock_new();
|
2019-05-01 18:02:43 +08:00
|
|
|
propdata->prop_names = lh_PROPERTY_STRING_new(&property_hash,
|
|
|
|
&property_cmp);
|
|
|
|
propdata->prop_values = lh_PROPERTY_STRING_new(&property_hash,
|
|
|
|
&property_cmp);
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifndef OPENSSL_SMALL_FOOTPRINT
|
2021-12-21 08:44:31 +08:00
|
|
|
propdata->prop_namelist = sk_OPENSSL_CSTRING_new_null();
|
|
|
|
propdata->prop_valuelist = sk_OPENSSL_CSTRING_new_null();
|
2022-01-01 09:43:31 +08:00
|
|
|
#endif
|
2021-12-21 08:44:31 +08:00
|
|
|
if (propdata->lock == NULL
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifndef OPENSSL_SMALL_FOOTPRINT
|
2021-12-21 08:44:31 +08:00
|
|
|
|| propdata->prop_namelist == NULL
|
2022-01-01 09:43:31 +08:00
|
|
|
|| propdata->prop_valuelist == NULL
|
|
|
|
#endif
|
|
|
|
|| propdata->prop_names == NULL
|
|
|
|
|| propdata->prop_values == NULL) {
|
2022-03-14 16:13:12 +08:00
|
|
|
ossl_property_string_data_free(propdata);
|
2021-12-21 08:44:31 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-01 18:02:43 +08:00
|
|
|
return propdata;
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:44:30 +08:00
|
|
|
static PROPERTY_STRING *new_property_string(const char *s,
|
|
|
|
OSSL_PROPERTY_IDX *pidx)
|
|
|
|
{
|
|
|
|
const size_t l = strlen(s);
|
|
|
|
PROPERTY_STRING *ps = OPENSSL_malloc(sizeof(*ps) + l);
|
|
|
|
|
|
|
|
if (ps != NULL) {
|
|
|
|
memcpy(ps->body, s, l + 1);
|
|
|
|
ps->s = ps->body;
|
|
|
|
ps->idx = ++*pidx;
|
|
|
|
if (ps->idx == 0) {
|
|
|
|
OPENSSL_free(ps);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:44:31 +08:00
|
|
|
static OSSL_PROPERTY_IDX ossl_property_string(OSSL_LIB_CTX *ctx, int name,
|
|
|
|
int create, const char *s)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
PROPERTY_STRING p, *ps, *ps_new;
|
2021-12-21 08:44:31 +08:00
|
|
|
PROP_TABLE *t;
|
|
|
|
OSSL_PROPERTY_IDX *pidx;
|
|
|
|
PROPERTY_STRING_DATA *propdata
|
2022-03-14 16:13:12 +08:00
|
|
|
= ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX);
|
2018-11-16 09:44:30 +08:00
|
|
|
|
2021-12-21 08:44:31 +08:00
|
|
|
if (propdata == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
t = name ? propdata->prop_names : propdata->prop_values;
|
2018-11-16 09:44:30 +08:00
|
|
|
p.s = s;
|
2021-12-21 08:44:31 +08:00
|
|
|
if (!CRYPTO_THREAD_read_lock(propdata->lock)) {
|
2021-06-23 12:18:07 +08:00
|
|
|
ERR_raise(ERR_LIB_CRYPTO, ERR_R_UNABLE_TO_GET_READ_LOCK);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-16 09:44:30 +08:00
|
|
|
ps = lh_PROPERTY_STRING_retrieve(t, &p);
|
2021-12-21 08:44:31 +08:00
|
|
|
if (ps == NULL && create) {
|
|
|
|
CRYPTO_THREAD_unlock(propdata->lock);
|
|
|
|
if (!CRYPTO_THREAD_write_lock(propdata->lock)) {
|
2021-06-23 12:18:07 +08:00
|
|
|
ERR_raise(ERR_LIB_CRYPTO, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-12-21 08:44:31 +08:00
|
|
|
pidx = name ? &propdata->prop_name_idx : &propdata->prop_value_idx;
|
2021-06-23 12:18:07 +08:00
|
|
|
ps = lh_PROPERTY_STRING_retrieve(t, &p);
|
|
|
|
if (ps == NULL && (ps_new = new_property_string(s, pidx)) != NULL) {
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifndef OPENSSL_SMALL_FOOTPRINT
|
|
|
|
STACK_OF(OPENSSL_CSTRING) *slist;
|
|
|
|
|
2021-12-21 08:44:31 +08:00
|
|
|
slist = name ? propdata->prop_namelist : propdata->prop_valuelist;
|
|
|
|
if (sk_OPENSSL_CSTRING_push(slist, ps_new->s) <= 0) {
|
|
|
|
property_free(ps_new);
|
|
|
|
CRYPTO_THREAD_unlock(propdata->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-01 09:43:31 +08:00
|
|
|
#endif
|
2018-11-16 09:44:30 +08:00
|
|
|
lh_PROPERTY_STRING_insert(t, ps_new);
|
|
|
|
if (lh_PROPERTY_STRING_error(t)) {
|
2021-12-21 08:44:31 +08:00
|
|
|
/*-
|
|
|
|
* Undo the previous push which means also decrementing the
|
|
|
|
* index and freeing the allocated storage.
|
|
|
|
*/
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifndef OPENSSL_SMALL_FOOTPRINT
|
2021-12-21 08:44:31 +08:00
|
|
|
sk_OPENSSL_CSTRING_pop(slist);
|
2022-01-01 09:43:31 +08:00
|
|
|
#endif
|
2018-11-16 09:44:30 +08:00
|
|
|
property_free(ps_new);
|
2021-12-21 08:44:31 +08:00
|
|
|
--*pidx;
|
|
|
|
CRYPTO_THREAD_unlock(propdata->lock);
|
2018-11-16 09:44:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ps = ps_new;
|
|
|
|
}
|
2021-06-23 12:18:07 +08:00
|
|
|
}
|
2021-12-21 08:44:31 +08:00
|
|
|
CRYPTO_THREAD_unlock(propdata->lock);
|
2018-11-16 09:44:30 +08:00
|
|
|
return ps != NULL ? ps->idx : 0;
|
|
|
|
}
|
|
|
|
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifdef OPENSSL_SMALL_FOOTPRINT
|
|
|
|
struct find_str_st {
|
|
|
|
const char *str;
|
|
|
|
OSSL_PROPERTY_IDX idx;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void find_str_fn(PROPERTY_STRING *prop, void *vfindstr)
|
|
|
|
{
|
|
|
|
struct find_str_st *findstr = vfindstr;
|
|
|
|
|
|
|
|
if (prop->idx == findstr->idx)
|
|
|
|
findstr->str = prop->s;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-05-07 23:42:53 +08:00
|
|
|
static const char *ossl_property_str(int name, OSSL_LIB_CTX *ctx,
|
|
|
|
OSSL_PROPERTY_IDX idx)
|
|
|
|
{
|
2021-12-21 08:44:31 +08:00
|
|
|
const char *r;
|
2021-05-07 23:42:53 +08:00
|
|
|
PROPERTY_STRING_DATA *propdata
|
2022-03-14 16:13:12 +08:00
|
|
|
= ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX);
|
2021-05-07 23:42:53 +08:00
|
|
|
|
|
|
|
if (propdata == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2021-06-23 12:18:07 +08:00
|
|
|
if (!CRYPTO_THREAD_read_lock(propdata->lock)) {
|
|
|
|
ERR_raise(ERR_LIB_CRYPTO, ERR_R_UNABLE_TO_GET_READ_LOCK);
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-01-01 09:43:31 +08:00
|
|
|
#ifdef OPENSSL_SMALL_FOOTPRINT
|
|
|
|
{
|
|
|
|
struct find_str_st findstr;
|
|
|
|
|
|
|
|
findstr.str = NULL;
|
|
|
|
findstr.idx = idx;
|
|
|
|
|
|
|
|
lh_PROPERTY_STRING_doall_arg(name ? propdata->prop_names
|
|
|
|
: propdata->prop_values,
|
|
|
|
find_str_fn, &findstr);
|
|
|
|
r = findstr.str;
|
|
|
|
}
|
|
|
|
#else
|
2021-12-21 08:44:31 +08:00
|
|
|
r = sk_OPENSSL_CSTRING_value(name ? propdata->prop_namelist
|
|
|
|
: propdata->prop_valuelist, idx - 1);
|
2022-01-01 09:43:31 +08:00
|
|
|
#endif
|
2021-06-23 12:18:07 +08:00
|
|
|
CRYPTO_THREAD_unlock(propdata->lock);
|
2021-05-07 23:42:53 +08:00
|
|
|
|
2021-12-21 08:44:31 +08:00
|
|
|
return r;
|
2021-05-07 23:42:53 +08:00
|
|
|
}
|
|
|
|
|
2021-06-23 12:18:07 +08:00
|
|
|
OSSL_PROPERTY_IDX ossl_property_name(OSSL_LIB_CTX *ctx, const char *s,
|
|
|
|
int create)
|
|
|
|
{
|
2021-12-21 08:44:31 +08:00
|
|
|
return ossl_property_string(ctx, 1, create, s);
|
2021-06-23 12:18:07 +08:00
|
|
|
}
|
|
|
|
|
2021-05-07 23:42:53 +08:00
|
|
|
const char *ossl_property_name_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx)
|
|
|
|
{
|
|
|
|
return ossl_property_str(1, ctx, idx);
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_PROPERTY_IDX ossl_property_value(OSSL_LIB_CTX *ctx, const char *s,
|
2019-05-01 18:02:43 +08:00
|
|
|
int create)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
2021-12-21 08:44:31 +08:00
|
|
|
return ossl_property_string(ctx, 0, create, s);
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
2021-05-07 23:42:53 +08:00
|
|
|
|
|
|
|
const char *ossl_property_value_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx)
|
|
|
|
{
|
|
|
|
return ossl_property_str(0, ctx, idx);
|
|
|
|
}
|