Update copyright year
[openssl.git] / crypto / property / defn_cache.c
1 /*
2  * Copyright 2019-2022 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 "internal/core.h"
17 #include "property_local.h"
18 #include "crypto/context.h"
19
20 /*
21  * Implement a property definition cache.
22  * These functions assume that they are called under a write lock.
23  * No attempt is made to clean out the cache, except when it is shut down.
24  */
25
26 typedef struct {
27     const char *prop;
28     OSSL_PROPERTY_LIST *defn;
29     char body[1];
30 } PROPERTY_DEFN_ELEM;
31
32 DEFINE_LHASH_OF(PROPERTY_DEFN_ELEM);
33
34 static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
35 {
36     return OPENSSL_LH_strhash(a->prop);
37 }
38
39 static 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
45 static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
46 {
47     ossl_property_free(elem->defn);
48     OPENSSL_free(elem);
49 }
50
51 void ossl_property_defns_free(void *vproperty_defns)
52 {
53     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
54
55     if (property_defns != NULL) {
56         lh_PROPERTY_DEFN_ELEM_doall(property_defns,
57                                     &property_defn_free);
58         lh_PROPERTY_DEFN_ELEM_free(property_defns);
59     }
60 }
61
62 void *ossl_property_defns_new(OSSL_LIB_CTX *ctx) {
63     return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
64 }
65
66 OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop)
67 {
68     PROPERTY_DEFN_ELEM elem, *r;
69     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
70
71     property_defns = ossl_lib_ctx_get_data(ctx,
72                                            OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
73     if (property_defns == NULL || !ossl_lib_ctx_read_lock(ctx))
74         return NULL;
75
76     elem.prop = prop;
77     r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
78     ossl_lib_ctx_unlock(ctx);
79     return r != NULL ? r->defn : NULL;
80 }
81
82 int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
83                        OSSL_PROPERTY_LIST *pl)
84 {
85     PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
86     size_t len;
87     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
88     int res = 1;
89
90     property_defns = ossl_lib_ctx_get_data(ctx,
91                                            OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
92     if (property_defns == NULL)
93         return 0;
94
95     if (prop == NULL)
96         return 1;
97
98     if (!ossl_lib_ctx_write_lock(ctx))
99         return 0;
100     if (pl == NULL) {
101         elem.prop = prop;
102         lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
103         goto end;
104     }
105     len = strlen(prop);
106     p = OPENSSL_malloc(sizeof(*p) + len);
107     if (p != NULL) {
108         p->prop = p->body;
109         p->defn = pl;
110         memcpy(p->body, prop, len + 1);
111         old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
112         if (old != NULL) {
113             property_defn_free(old);
114             goto end;
115         }
116         if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
117             goto end;
118     }
119     OPENSSL_free(p);
120     res = 0;
121  end:
122     ossl_lib_ctx_unlock(ctx);
123     return res;
124 }