ea4447621326f68ddc99df96af06b0508dbc4803
[openssl.git] / crypto / property / defn_cache.c
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_local.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
24 typedef struct {
25     const char *prop;
26     OSSL_PROPERTY_LIST *defn;
27     char body[1];
28 } PROPERTY_DEFN_ELEM;
29
30 DEFINE_LHASH_OF(PROPERTY_DEFN_ELEM);
31
32 static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
33 {
34     return OPENSSL_LH_strhash(a->prop);
35 }
36
37 static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
38                              const PROPERTY_DEFN_ELEM *b)
39 {
40     return strcmp(a->prop, b->prop);
41 }
42
43 static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
44 {
45     ossl_property_free(elem->defn);
46     OPENSSL_free(elem);
47 }
48
49 static void property_defns_free(void *vproperty_defns)
50 {
51     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
52
53     if (property_defns != NULL) {
54         lh_PROPERTY_DEFN_ELEM_doall(property_defns,
55                                     &property_defn_free);
56         lh_PROPERTY_DEFN_ELEM_free(property_defns);
57     }
58 }
59
60 static void *property_defns_new(OSSL_LIB_CTX *ctx) {
61     return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
62 }
63
64 static const OSSL_LIB_CTX_METHOD property_defns_method = {
65     property_defns_new,
66     property_defns_free,
67 };
68
69 OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop)
70 {
71     PROPERTY_DEFN_ELEM elem, *r;
72     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
73
74     property_defns = ossl_lib_ctx_get_data(ctx,
75                                            OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
76                                            &property_defns_method);
77     if (property_defns == NULL)
78         return NULL;
79
80     elem.prop = prop;
81     r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
82     return r != NULL ? r->defn : NULL;
83 }
84
85 int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
86                        OSSL_PROPERTY_LIST *pl)
87 {
88     PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
89     size_t len;
90     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
91
92     property_defns = ossl_lib_ctx_get_data(ctx,
93                                            OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
94                                            &property_defns_method);
95     if (property_defns == NULL)
96         return 0;
97
98     if (prop == NULL)
99         return 1;
100
101     if (pl == NULL) {
102         elem.prop = prop;
103         lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
104         return 1;
105     }
106     len = strlen(prop);
107     p = OPENSSL_malloc(sizeof(*p) + len);
108     if (p != NULL) {
109         p->prop = p->body;
110         p->defn = pl;
111         memcpy(p->body, prop, len + 1);
112         old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
113         if (old != NULL) {
114             property_defn_free(old);
115             return 1;
116         }
117         if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
118             return 1;
119     }
120     OPENSSL_free(p);
121     return 0;
122 }