• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 /*
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.
23  */
24 
25 typedef struct {
26     const char *prop;
27     OSSL_PROPERTY_LIST *defn;
28     char body[1];
29 } PROPERTY_DEFN_ELEM;
30 
31 DEFINE_LHASH_OF(PROPERTY_DEFN_ELEM);
32 
property_defn_hash(const PROPERTY_DEFN_ELEM * a)33 static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
34 {
35     return OPENSSL_LH_strhash(a->prop);
36 }
37 
property_defn_cmp(const PROPERTY_DEFN_ELEM * a,const PROPERTY_DEFN_ELEM * b)38 static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
39                              const PROPERTY_DEFN_ELEM *b)
40 {
41     return strcmp(a->prop, b->prop);
42 }
43 
property_defn_free(PROPERTY_DEFN_ELEM * elem)44 static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
45 {
46     ossl_property_free(elem->defn);
47     OPENSSL_free(elem);
48 }
49 
property_defns_free(void * vproperty_defns)50 static void property_defns_free(void *vproperty_defns)
51 {
52     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
53 
54     if (property_defns != NULL) {
55         lh_PROPERTY_DEFN_ELEM_doall(property_defns,
56                                     &property_defn_free);
57         lh_PROPERTY_DEFN_ELEM_free(property_defns);
58     }
59 }
60 
property_defns_new(OSSL_LIB_CTX * ctx)61 static void *property_defns_new(OSSL_LIB_CTX *ctx) {
62     return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
63 }
64 
65 static const OSSL_LIB_CTX_METHOD property_defns_method = {
66     OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
67     property_defns_new,
68     property_defns_free,
69 };
70 
ossl_prop_defn_get(OSSL_LIB_CTX * ctx,const char * prop)71 OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop)
72 {
73     PROPERTY_DEFN_ELEM elem, *r;
74     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
75 
76     property_defns = ossl_lib_ctx_get_data(ctx,
77                                            OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
78                                            &property_defns_method);
79     if (property_defns == NULL || !ossl_lib_ctx_read_lock(ctx))
80         return NULL;
81 
82     elem.prop = prop;
83     r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
84     ossl_lib_ctx_unlock(ctx);
85     return r != NULL ? r->defn : NULL;
86 }
87 
88 /*
89  * Cache the property list for a given property string. Callers of this function
90  * should call ossl_prop_defn_get first to ensure that there is no existing
91  * cache entry for this property string.
92  */
ossl_prop_defn_set(OSSL_LIB_CTX * ctx,const char * prop,OSSL_PROPERTY_LIST * pl)93 int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
94                        OSSL_PROPERTY_LIST *pl)
95 {
96     PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
97     size_t len;
98     LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
99     int res = 1;
100 
101     property_defns = ossl_lib_ctx_get_data(ctx,
102                                            OSSL_LIB_CTX_PROPERTY_DEFN_INDEX,
103                                            &property_defns_method);
104     if (property_defns == NULL)
105         return 0;
106 
107     if (prop == NULL)
108         return 1;
109 
110     if (!ossl_lib_ctx_write_lock(ctx))
111         return 0;
112     if (pl == NULL) {
113         elem.prop = prop;
114         lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
115         goto end;
116     }
117     len = strlen(prop);
118     p = OPENSSL_malloc(sizeof(*p) + len);
119     if (p != NULL) {
120         p->prop = p->body;
121         p->defn = pl;
122         memcpy(p->body, prop, len + 1);
123         old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
124         if (!ossl_assert(old == NULL)) {
125             /*
126              * This should not happen. Any caller of ossl_prop_defn_set should
127              * have called ossl_prop_defn_get first - so we should know that
128              * there is no existing entry. If we get here we have a bug. We
129              * deliberately leak the |old| reference in order to avoid a crash
130              * if there are any existing users of it.
131              */
132             goto end;
133         }
134         if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
135             goto end;
136     }
137     OPENSSL_free(p);
138     res = 0;
139  end:
140     ossl_lib_ctx_unlock(ctx);
141     return res;
142 }
143