]>
Commit | Line | Data |
---|---|---|
c13d2ab4 RL |
1 | /* |
2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include <stddef.h> | |
11 | #include <openssl/ossl_typ.h> | |
12 | #include <openssl/evp.h> | |
13 | #include <openssl/core.h> | |
14 | #include "internal/cryptlib.h" | |
15 | #include "internal/thread_once.h" | |
c13d2ab4 RL |
16 | #include "internal/property.h" |
17 | #include "internal/core.h" | |
baff732d | 18 | #include "internal/namemap.h" |
c13d2ab4 RL |
19 | #include "internal/evp_int.h" /* evp_locl.h needs it */ |
20 | #include "evp_locl.h" | |
21 | ||
c13d2ab4 RL |
22 | static void default_method_store_free(void *vstore) |
23 | { | |
24 | ossl_method_store_free(vstore); | |
25 | } | |
26 | ||
1aedc35f | 27 | static void *default_method_store_new(OPENSSL_CTX *ctx) |
c13d2ab4 | 28 | { |
1aedc35f | 29 | return ossl_method_store_new(ctx); |
c13d2ab4 RL |
30 | } |
31 | ||
32 | ||
33 | static const OPENSSL_CTX_METHOD default_method_store_method = { | |
34 | default_method_store_new, | |
35 | default_method_store_free, | |
36 | }; | |
37 | ||
c13d2ab4 RL |
38 | /* Data to be passed through ossl_method_construct() */ |
39 | struct method_data_st { | |
baff732d | 40 | OPENSSL_CTX *libctx; |
c13d2ab4 | 41 | const char *name; |
c13d2ab4 | 42 | OSSL_METHOD_CONSTRUCT_METHOD *mcm; |
0211740f | 43 | void *(*method_from_dispatch)(const OSSL_DISPATCH *, OSSL_PROVIDER *); |
c13d2ab4 RL |
44 | int (*refcnt_up_method)(void *method); |
45 | void (*destruct_method)(void *method); | |
46 | }; | |
47 | ||
48 | /* | |
49 | * Generic routines to fetch / create EVP methods with ossl_method_construct() | |
50 | */ | |
1aedc35f | 51 | static void *alloc_tmp_method_store(OPENSSL_CTX *ctx) |
c13d2ab4 | 52 | { |
1aedc35f | 53 | return ossl_method_store_new(ctx); |
c13d2ab4 RL |
54 | } |
55 | ||
56 | static void dealloc_tmp_method_store(void *store) | |
57 | { | |
58 | if (store != NULL) | |
59 | ossl_method_store_free(store); | |
60 | } | |
61 | ||
cb929645 | 62 | static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx) |
c13d2ab4 | 63 | { |
1aedc35f MC |
64 | return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX, |
65 | &default_method_store_method); | |
c13d2ab4 RL |
66 | } |
67 | ||
2ccb1b4e RL |
68 | /* |
69 | * To identity the method in the method store, we mix the name identity | |
70 | * with the operation identity, with the assumption that we don't have | |
71 | * more than 2^24 names or more than 2^8 operation types. | |
72 | * | |
73 | * The resulting identity is a 32-bit integer, composed like this: | |
74 | * | |
75 | * +---------24 bits--------+-8 bits-+ | |
76 | * | name identity | op id | | |
77 | * +------------------------+--------+ | |
78 | */ | |
79 | static uint32_t method_id(unsigned int operation_id, unsigned int name_id) | |
80 | { | |
81 | if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8)) | |
82 | || !ossl_assert(name_id > 0 && operation_id > 0)) | |
83 | return 0; | |
84 | return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF); | |
85 | } | |
86 | ||
c13d2ab4 | 87 | static void *get_method_from_store(OPENSSL_CTX *libctx, void *store, |
2ccb1b4e RL |
88 | int operation_id, const char *name, |
89 | const char *propquery, void *data) | |
c13d2ab4 RL |
90 | { |
91 | struct method_data_st *methdata = data; | |
92 | void *method = NULL; | |
2e49c054 | 93 | OSSL_NAMEMAP *namemap; |
2ccb1b4e RL |
94 | int nameid; |
95 | uint32_t methid; | |
c13d2ab4 RL |
96 | |
97 | if (store == NULL | |
98 | && (store = get_default_method_store(libctx)) == NULL) | |
99 | return NULL; | |
100 | ||
2e49c054 | 101 | if ((namemap = ossl_namemap_stored(libctx)) == NULL |
651d4418 | 102 | || (nameid = ossl_namemap_name2num(namemap, name)) == 0 |
2ccb1b4e | 103 | || (methid = method_id(operation_id, nameid)) == 0) |
2e49c054 RL |
104 | return NULL; |
105 | ||
2ccb1b4e | 106 | (void)ossl_method_store_fetch(store, methid, propquery, &method); |
c13d2ab4 RL |
107 | |
108 | if (method != NULL | |
109 | && !methdata->refcnt_up_method(method)) { | |
110 | method = NULL; | |
111 | } | |
112 | return method; | |
113 | } | |
114 | ||
115 | static int put_method_in_store(OPENSSL_CTX *libctx, void *store, | |
2ccb1b4e RL |
116 | void *method, int operation_id, |
117 | const char *name, const char *propdef, | |
118 | void *data) | |
c13d2ab4 RL |
119 | { |
120 | struct method_data_st *methdata = data; | |
2e49c054 | 121 | OSSL_NAMEMAP *namemap; |
2ccb1b4e RL |
122 | int nameid; |
123 | uint32_t methid; | |
dc46e3dd | 124 | |
2e49c054 | 125 | if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL |
651d4418 | 126 | || (nameid = ossl_namemap_add(namemap, 0, name)) == 0 |
2ccb1b4e | 127 | || (methid = method_id(operation_id, nameid)) == 0) |
dc46e3dd | 128 | return 0; |
c13d2ab4 RL |
129 | |
130 | if (store == NULL | |
131 | && (store = get_default_method_store(libctx)) == NULL) | |
132 | return 0; | |
133 | ||
134 | if (methdata->refcnt_up_method(method) | |
2ccb1b4e | 135 | && ossl_method_store_add(store, methid, propdef, method, |
c13d2ab4 RL |
136 | methdata->destruct_method)) |
137 | return 1; | |
138 | return 0; | |
139 | } | |
140 | ||
2e49c054 RL |
141 | static void *construct_method(const char *name, const OSSL_DISPATCH *fns, |
142 | OSSL_PROVIDER *prov, void *data) | |
c13d2ab4 RL |
143 | { |
144 | struct method_data_st *methdata = data; | |
c13d2ab4 | 145 | |
0211740f | 146 | return methdata->method_from_dispatch(fns, prov); |
c13d2ab4 RL |
147 | } |
148 | ||
149 | static void destruct_method(void *method, void *data) | |
150 | { | |
151 | struct method_data_st *methdata = data; | |
152 | ||
153 | methdata->destruct_method(method); | |
154 | } | |
155 | ||
156 | void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id, | |
2e49c054 | 157 | const char *name, const char *properties, |
0211740f | 158 | void *(*new_method)(const OSSL_DISPATCH *fns, |
c13d2ab4 RL |
159 | OSSL_PROVIDER *prov), |
160 | int (*upref_method)(void *), | |
0211740f | 161 | void (*free_method)(void *)) |
c13d2ab4 | 162 | { |
e019da7b | 163 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); |
baff732d | 164 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
2ccb1b4e RL |
165 | int nameid = 0; |
166 | uint32_t methid = 0; | |
c13d2ab4 RL |
167 | void *method = NULL; |
168 | ||
baff732d | 169 | if (store == NULL || namemap == NULL) |
e019da7b RL |
170 | return NULL; |
171 | ||
2ccb1b4e RL |
172 | /* |
173 | * If there's ever an operation_id == 0 passed, we have an internal | |
174 | * programming error. | |
175 | */ | |
176 | if (!ossl_assert(operation_id > 0)) | |
177 | return NULL; | |
178 | ||
179 | /* | |
180 | * method_id returns 0 if we have too many operations (more than | |
181 | * about 2^8) or too many names (more than about 2^24). In that | |
182 | * case, we can't create any new method. | |
183 | */ | |
651d4418 | 184 | if ((nameid = ossl_namemap_name2num(namemap, name)) != 0 |
2ccb1b4e RL |
185 | && (methid = method_id(operation_id, nameid)) == 0) |
186 | return NULL; | |
187 | ||
188 | if (nameid == 0 | |
189 | || !ossl_method_store_cache_get(store, methid, properties, | |
190 | &method)) { | |
c13d2ab4 RL |
191 | OSSL_METHOD_CONSTRUCT_METHOD mcm = { |
192 | alloc_tmp_method_store, | |
193 | dealloc_tmp_method_store, | |
194 | get_method_from_store, | |
195 | put_method_in_store, | |
196 | construct_method, | |
197 | destruct_method | |
198 | }; | |
199 | struct method_data_st mcmdata; | |
200 | ||
c13d2ab4 | 201 | mcmdata.mcm = &mcm; |
baff732d | 202 | mcmdata.libctx = libctx; |
d5e5e2ff | 203 | mcmdata.name = name; |
c13d2ab4 RL |
204 | mcmdata.method_from_dispatch = new_method; |
205 | mcmdata.destruct_method = free_method; | |
206 | mcmdata.refcnt_up_method = upref_method; | |
207 | mcmdata.destruct_method = free_method; | |
2ccb1b4e RL |
208 | if ((method = ossl_method_construct(libctx, operation_id, name, |
209 | properties, 0 /* !force_cache */, | |
210 | &mcm, &mcmdata)) == NULL) { | |
211 | /* | |
212 | * If construction did create a method for us, we know that | |
213 | * there is a correct nameid and methodid, since those have | |
214 | * already been calculated in get_method_from_store() and | |
215 | * put_method_in_store() above. | |
216 | */ | |
651d4418 | 217 | nameid = ossl_namemap_name2num(namemap, name); |
2ccb1b4e RL |
218 | methid = method_id(operation_id, nameid); |
219 | ossl_method_store_cache_set(store, methid, properties, method); | |
220 | } | |
e019da7b RL |
221 | } else { |
222 | upref_method(method); | |
c13d2ab4 RL |
223 | } |
224 | ||
225 | return method; | |
226 | } | |
cb929645 RL |
227 | |
228 | int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq) | |
229 | { | |
230 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); | |
231 | ||
232 | if (store != NULL) | |
233 | return ossl_method_store_set_global_properties(store, propq); | |
234 | EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR); | |
235 | return 0; | |
236 | } |