2 * Copyright 2019 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 "property_lcl.h"
19 * Implement a property definition cache.
20 * These functions assume that they are called under a write lock.
21 * No attempt is made to clean out the cache, except when it is shut down.
26 OSSL_PROPERTY_LIST *defn;
30 DEFINE_LHASH_OF(PROPERTY_DEFN_ELEM);
32 static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
34 return OPENSSL_LH_strhash(a->prop);
37 static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
38 const PROPERTY_DEFN_ELEM *b)
40 return strcmp(a->prop, b->prop);
43 static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
45 ossl_property_free(elem->defn);
49 static void property_defns_free(void *vproperty_defns)
51 LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
53 if (property_defns != NULL) {
54 lh_PROPERTY_DEFN_ELEM_doall(property_defns,
56 lh_PROPERTY_DEFN_ELEM_free(property_defns);
60 static void *property_defns_new(OPENSSL_CTX *ctx) {
61 return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
64 static const OPENSSL_CTX_METHOD property_defns_method = {
69 OSSL_PROPERTY_LIST *ossl_prop_defn_get(OPENSSL_CTX *ctx, const char *prop)
71 PROPERTY_DEFN_ELEM elem, *r;
72 LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
74 property_defns = openssl_ctx_get_data(ctx, OPENSSL_CTX_PROPERTY_DEFN_INDEX,
75 &property_defns_method);
76 if (property_defns == NULL)
80 r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
81 return r != NULL ? r->defn : NULL;
84 int ossl_prop_defn_set(OPENSSL_CTX *ctx, const char *prop,
85 OSSL_PROPERTY_LIST *pl)
87 PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
89 LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
91 property_defns = openssl_ctx_get_data(ctx, OPENSSL_CTX_PROPERTY_DEFN_INDEX,
92 &property_defns_method);
93 if (property_defns == NULL)
101 lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
105 p = OPENSSL_malloc(sizeof(*p) + len);
109 memcpy(p->body, prop, len + 1);
110 old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
112 property_defn_free(old);
115 if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))