]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/property/defn_cache.c
Add support for openssl_ctx_run_once and openssl_ctx_onfree
[thirdparty/openssl.git] / crypto / property / defn_cache.c
CommitLineData
1bdbdaff
P
1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4 *
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
9 */
10
11#include <string.h>
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"
17
18/*
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.
22 */
23
24typedef struct {
25 const char *prop;
26 OSSL_PROPERTY_LIST *defn;
27 char body[1];
28} PROPERTY_DEFN_ELEM;
29
30DEFINE_LHASH_OF(PROPERTY_DEFN_ELEM);
31
32static LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = NULL;
33
34static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
35{
36 return OPENSSL_LH_strhash(a->prop);
37}
38
39static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
40 const PROPERTY_DEFN_ELEM *b)
41{
42 return strcmp(a->prop, b->prop);
43}
44
45static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
46{
47 ossl_property_free(elem->defn);
48 OPENSSL_free(elem);
49}
50
51int ossl_prop_defn_init(void)
52{
53 property_defns = lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash,
54 &property_defn_cmp);
55 return property_defns != NULL;
56}
57
58void ossl_prop_defn_cleanup(void)
59{
60 if (property_defns != NULL) {
61 lh_PROPERTY_DEFN_ELEM_doall(property_defns, &property_defn_free);
62 lh_PROPERTY_DEFN_ELEM_free(property_defns);
63 property_defns = NULL;
64 }
65}
66
67OSSL_PROPERTY_LIST *ossl_prop_defn_get(const char *prop)
68{
69 PROPERTY_DEFN_ELEM elem, *r;
70
71 elem.prop = prop;
72 r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
73 return r != NULL ? r->defn : NULL;
74}
75
76int ossl_prop_defn_set(const char *prop, OSSL_PROPERTY_LIST *pl)
77{
78 PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
79 size_t len;
80
81 if (prop == NULL)
82 return 1;
83
84 if (pl == NULL) {
85 elem.prop = prop;
86 lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
87 return 1;
88 }
89 len = strlen(prop);
90 p = OPENSSL_malloc(sizeof(*p) + len);
91 if (p != NULL) {
92 p->prop = p->body;
93 p->defn = pl;
94 memcpy(p->body, prop, len + 1);
95 old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
96 if (old != NULL) {
97 property_defn_free(old);
98 return 1;
99 }
100 if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
101 return 1;
102 }
103 OPENSSL_free(p);
104 return 0;
105}