2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include <openssl/crypto.h>
13 #include <openssl/lhash.h>
14 #include "crypto/lhash.h"
15 #include "property_local.h"
18 * Property strings are a consolidation of all strings seen by the property
19 * subsystem. There are two name spaces to keep property names separate from
20 * property values (numeric values are not expected to be cached however).
21 * They allow a rapid conversion from a string to a unique index and any
22 * subsequent string comparison can be done via an integer compare.
24 * This implementation uses OpenSSL's standard hash table. There are more
25 * space and time efficient algorithms if this becomes a bottleneck.
30 OSSL_PROPERTY_IDX idx;
34 DEFINE_LHASH_OF(PROPERTY_STRING);
35 typedef LHASH_OF(PROPERTY_STRING) PROP_TABLE;
38 PROP_TABLE *prop_names;
39 PROP_TABLE *prop_values;
40 OSSL_PROPERTY_IDX prop_name_idx;
41 OSSL_PROPERTY_IDX prop_value_idx;
42 } PROPERTY_STRING_DATA;
44 static unsigned long property_hash(const PROPERTY_STRING *a)
46 return OPENSSL_LH_strhash(a->s);
49 static int property_cmp(const PROPERTY_STRING *a, const PROPERTY_STRING *b)
51 return strcmp(a->s, b->s);
54 static void property_free(PROPERTY_STRING *ps)
59 static void property_table_free(PROP_TABLE **pt)
64 lh_PROPERTY_STRING_doall(t, &property_free);
65 lh_PROPERTY_STRING_free(t);
70 static void property_string_data_free(void *vpropdata)
72 PROPERTY_STRING_DATA *propdata = vpropdata;
77 property_table_free(&propdata->prop_names);
78 property_table_free(&propdata->prop_values);
79 propdata->prop_name_idx = propdata->prop_value_idx = 0;
81 OPENSSL_free(propdata);
84 static void *property_string_data_new(OSSL_LIB_CTX *ctx) {
85 PROPERTY_STRING_DATA *propdata = OPENSSL_zalloc(sizeof(*propdata));
90 propdata->prop_names = lh_PROPERTY_STRING_new(&property_hash,
92 if (propdata->prop_names == NULL)
95 propdata->prop_values = lh_PROPERTY_STRING_new(&property_hash,
97 if (propdata->prop_values == NULL)
103 property_string_data_free(propdata);
107 static const OSSL_LIB_CTX_METHOD property_string_data_method = {
108 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
109 property_string_data_new,
110 property_string_data_free,
113 static PROPERTY_STRING *new_property_string(const char *s,
114 OSSL_PROPERTY_IDX *pidx)
116 const size_t l = strlen(s);
117 PROPERTY_STRING *ps = OPENSSL_malloc(sizeof(*ps) + l);
120 memcpy(ps->body, s, l + 1);
131 static OSSL_PROPERTY_IDX ossl_property_string(PROP_TABLE *t,
132 OSSL_PROPERTY_IDX *pidx,
135 PROPERTY_STRING p, *ps, *ps_new;
138 ps = lh_PROPERTY_STRING_retrieve(t, &p);
139 if (ps == NULL && pidx != NULL)
140 if ((ps_new = new_property_string(s, pidx)) != NULL) {
141 lh_PROPERTY_STRING_insert(t, ps_new);
142 if (lh_PROPERTY_STRING_error(t)) {
143 property_free(ps_new);
148 return ps != NULL ? ps->idx : 0;
151 OSSL_PROPERTY_IDX ossl_property_name(OSSL_LIB_CTX *ctx, const char *s,
154 PROPERTY_STRING_DATA *propdata
155 = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
156 &property_string_data_method);
158 if (propdata == NULL)
160 return ossl_property_string(propdata->prop_names,
161 create ? &propdata->prop_name_idx : NULL,
167 OSSL_PROPERTY_IDX idx;
170 static void find_str_fn(PROPERTY_STRING *prop, void *vfindstr)
172 struct find_str_st *findstr = vfindstr;
174 if (prop->idx == findstr->idx)
175 findstr->str = prop->s;
178 static const char *ossl_property_str(int name, OSSL_LIB_CTX *ctx,
179 OSSL_PROPERTY_IDX idx)
181 struct find_str_st findstr;
182 PROPERTY_STRING_DATA *propdata
183 = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
184 &property_string_data_method);
186 if (propdata == NULL)
192 lh_PROPERTY_STRING_doall_arg(name ? propdata->prop_names
193 : propdata->prop_values,
194 find_str_fn, &findstr);
199 const char *ossl_property_name_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx)
201 return ossl_property_str(1, ctx, idx);
204 OSSL_PROPERTY_IDX ossl_property_value(OSSL_LIB_CTX *ctx, const char *s,
207 PROPERTY_STRING_DATA *propdata
208 = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
209 &property_string_data_method);
211 if (propdata == NULL)
213 return ossl_property_string(propdata->prop_values,
214 create ? &propdata->prop_value_idx : NULL,
218 const char *ossl_property_value_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx)
220 return ossl_property_str(0, ctx, idx);