EVP: Modify EVP_PKEY_export() to handle legacy EVP_PKEYs
[openssl.git] / crypto / property / property_string.c
1 /*
2  * Copyright 2019-2020 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/crypto.h>
13 #include <openssl/lhash.h>
14 #include "crypto/lhash.h"
15 #include "property_local.h"
16
17 /*
18  * Property strings are a consolidation of all strings seen by the property
19  * subsystem.  There are two name spaces to keep property names separate from
20  * property values (numeric values are not expected to be cached however).
21  * They allow a rapid conversion from a string to a unique index and any
22  * subsequent string comparison can be done via an integer compare.
23  *
24  * This implementation uses OpenSSL's standard hash table.  There are more
25  * space and time efficient algorithms if this becomes a bottleneck.
26  */
27
28 typedef struct {
29     const char *s;
30     OSSL_PROPERTY_IDX idx;
31     char body[1];
32 } PROPERTY_STRING;
33
34 DEFINE_LHASH_OF(PROPERTY_STRING);
35 typedef LHASH_OF(PROPERTY_STRING) PROP_TABLE;
36
37 typedef struct {
38     PROP_TABLE *prop_names;
39     PROP_TABLE *prop_values;
40     OSSL_PROPERTY_IDX prop_name_idx;
41     OSSL_PROPERTY_IDX prop_value_idx;
42 } PROPERTY_STRING_DATA;
43
44 static unsigned long property_hash(const PROPERTY_STRING *a)
45 {
46     return OPENSSL_LH_strhash(a->s);
47 }
48
49 static int property_cmp(const PROPERTY_STRING *a, const PROPERTY_STRING *b)
50 {
51     return strcmp(a->s, b->s);
52 }
53
54 static void property_free(PROPERTY_STRING *ps)
55 {
56     OPENSSL_free(ps);
57 }
58
59 static void property_table_free(PROP_TABLE **pt)
60 {
61     PROP_TABLE *t = *pt;
62
63     if (t != NULL) {
64         lh_PROPERTY_STRING_doall(t, &property_free);
65         lh_PROPERTY_STRING_free(t);
66         *pt = NULL;
67     }
68 }
69
70 static void property_string_data_free(void *vpropdata)
71 {
72     PROPERTY_STRING_DATA *propdata = vpropdata;
73
74     if (propdata == NULL)
75         return;
76
77     property_table_free(&propdata->prop_names);
78     property_table_free(&propdata->prop_values);
79     propdata->prop_name_idx = propdata->prop_value_idx = 0;
80
81     OPENSSL_free(propdata);
82 }
83
84 static void *property_string_data_new(OSSL_LIB_CTX *ctx) {
85     PROPERTY_STRING_DATA *propdata = OPENSSL_zalloc(sizeof(*propdata));
86
87     if (propdata == NULL)
88         return NULL;
89
90     propdata->prop_names = lh_PROPERTY_STRING_new(&property_hash,
91                                                   &property_cmp);
92     if (propdata->prop_names == NULL)
93         goto err;
94
95     propdata->prop_values = lh_PROPERTY_STRING_new(&property_hash,
96                                                    &property_cmp);
97     if (propdata->prop_values == NULL)
98         goto err;
99
100     return propdata;
101
102 err:
103     property_string_data_free(propdata);
104     return NULL;
105 }
106
107 static const OSSL_LIB_CTX_METHOD property_string_data_method = {
108     OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
109     property_string_data_new,
110     property_string_data_free,
111 };
112
113 static PROPERTY_STRING *new_property_string(const char *s,
114                                             OSSL_PROPERTY_IDX *pidx)
115 {
116     const size_t l = strlen(s);
117     PROPERTY_STRING *ps = OPENSSL_malloc(sizeof(*ps) + l);
118
119     if (ps != NULL) {
120         memcpy(ps->body, s, l + 1);
121         ps->s = ps->body;
122         ps->idx = ++*pidx;
123         if (ps->idx == 0) {
124             OPENSSL_free(ps);
125             return NULL;
126         }
127     }
128     return ps;
129 }
130
131 static OSSL_PROPERTY_IDX ossl_property_string(PROP_TABLE *t,
132                                               OSSL_PROPERTY_IDX *pidx,
133                                               const char *s)
134 {
135     PROPERTY_STRING p, *ps, *ps_new;
136
137     p.s = s;
138     ps = lh_PROPERTY_STRING_retrieve(t, &p);
139     if (ps == NULL && pidx != NULL)
140         if ((ps_new = new_property_string(s, pidx)) != NULL) {
141             lh_PROPERTY_STRING_insert(t, ps_new);
142             if (lh_PROPERTY_STRING_error(t)) {
143                 property_free(ps_new);
144                 return 0;
145             }
146             ps = ps_new;
147         }
148     return ps != NULL ? ps->idx : 0;
149 }
150
151 OSSL_PROPERTY_IDX ossl_property_name(OSSL_LIB_CTX *ctx, const char *s,
152                                      int create)
153 {
154     PROPERTY_STRING_DATA *propdata
155         = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
156                                 &property_string_data_method);
157
158     if (propdata == NULL)
159         return 0;
160     return ossl_property_string(propdata->prop_names,
161                                 create ? &propdata->prop_name_idx : NULL,
162                                 s);
163 }
164
165 struct find_str_st {
166     const char *str;
167     OSSL_PROPERTY_IDX idx;
168 };
169
170 static void find_str_fn(PROPERTY_STRING *prop, void *vfindstr)
171 {
172     struct find_str_st *findstr = vfindstr;
173
174     if (prop->idx == findstr->idx)
175         findstr->str = prop->s;
176 }
177
178 static const char *ossl_property_str(int name, OSSL_LIB_CTX *ctx,
179                                      OSSL_PROPERTY_IDX idx)
180 {
181     struct find_str_st findstr;
182     PROPERTY_STRING_DATA *propdata
183         = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
184                                 &property_string_data_method);
185
186     if (propdata == NULL)
187         return NULL;
188
189     findstr.str = NULL;
190     findstr.idx = idx;
191
192     lh_PROPERTY_STRING_doall_arg(name ? propdata->prop_names
193                                       : propdata->prop_values,
194                                  find_str_fn, &findstr);
195
196     return findstr.str;
197 }
198
199 const char *ossl_property_name_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx)
200 {
201     return ossl_property_str(1, ctx, idx);
202 }
203
204 OSSL_PROPERTY_IDX ossl_property_value(OSSL_LIB_CTX *ctx, const char *s,
205                                       int create)
206 {
207     PROPERTY_STRING_DATA *propdata
208         = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX,
209                                 &property_string_data_method);
210
211     if (propdata == NULL)
212         return 0;
213     return ossl_property_string(propdata->prop_values,
214                                 create ? &propdata->prop_value_idx : NULL,
215                                 s);
216 }
217
218 const char *ossl_property_value_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx)
219 {
220     return ossl_property_str(0, ctx, idx);
221 }