2 * Copyright 2019-2020 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/err.h>
13 #include <openssl/lhash.h>
14 #include "internal/propertyerr.h"
15 #include "internal/property.h"
16 #include "internal/core.h"
17 #include "property_local.h"
20 * Implement a property definition cache.
21 * These functions assume that they are called under a write lock.
22 * No attempt is made to clean out the cache, except when it is shut down.
27 OSSL_PROPERTY_LIST *defn;
31 DEFINE_LHASH_OF(PROPERTY_DEFN_ELEM);
33 static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
35 return OPENSSL_LH_strhash(a->prop);
38 static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
39 const PROPERTY_DEFN_ELEM *b)
41 return strcmp(a->prop, b->prop);
44 static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
46 ossl_property_free(elem->defn);
50 static void property_defns_free(void *vproperty_defns)
52 LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
54 if (property_defns != NULL) {
55 lh_PROPERTY_DEFN_ELEM_doall(property_defns,
57 lh_PROPERTY_DEFN_ELEM_free(property_defns);
61 static void *property_defns_new(OSSL_LIB_CTX *ctx) {
62 return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
65 static const OSSL_LIB_CTX_METHOD property_defns_method = {
70 OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop)
72 PROPERTY_DEFN_ELEM elem, *r;
73 LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
75 property_defns = ossl_lib_ctx_get_data(ctx,
76 OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
77 &property_defns_method);
78 if (property_defns == NULL || !ossl_lib_ctx_read_lock(ctx))
82 r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
83 ossl_lib_ctx_unlock(ctx);
84 return r != NULL ? r->defn : NULL;
87 int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
88 OSSL_PROPERTY_LIST *pl)
90 PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
92 LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
95 property_defns = ossl_lib_ctx_get_data(ctx,
96 OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
97 &property_defns_method);
98 if (property_defns == NULL)
104 if (!ossl_lib_ctx_write_lock(ctx))
108 lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
112 p = OPENSSL_malloc(sizeof(*p) + len);
116 memcpy(p->body, prop, len + 1);
117 old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
119 property_defn_free(old);
122 if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
128 ossl_lib_ctx_unlock(ctx);