]>
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; |
0211740f | 42 | int id; |
c13d2ab4 | 43 | OSSL_METHOD_CONSTRUCT_METHOD *mcm; |
0211740f | 44 | void *(*method_from_dispatch)(const OSSL_DISPATCH *, OSSL_PROVIDER *); |
c13d2ab4 RL |
45 | int (*refcnt_up_method)(void *method); |
46 | void (*destruct_method)(void *method); | |
47 | }; | |
48 | ||
49 | /* | |
50 | * Generic routines to fetch / create EVP methods with ossl_method_construct() | |
51 | */ | |
1aedc35f | 52 | static void *alloc_tmp_method_store(OPENSSL_CTX *ctx) |
c13d2ab4 | 53 | { |
1aedc35f | 54 | return ossl_method_store_new(ctx); |
c13d2ab4 RL |
55 | } |
56 | ||
57 | static void dealloc_tmp_method_store(void *store) | |
58 | { | |
59 | if (store != NULL) | |
60 | ossl_method_store_free(store); | |
61 | } | |
62 | ||
cb929645 | 63 | static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx) |
c13d2ab4 | 64 | { |
1aedc35f MC |
65 | return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX, |
66 | &default_method_store_method); | |
c13d2ab4 RL |
67 | } |
68 | ||
69 | static void *get_method_from_store(OPENSSL_CTX *libctx, void *store, | |
2e49c054 RL |
70 | const char *name, const char *propquery, |
71 | void *data) | |
c13d2ab4 RL |
72 | { |
73 | struct method_data_st *methdata = data; | |
74 | void *method = NULL; | |
2e49c054 RL |
75 | OSSL_NAMEMAP *namemap; |
76 | int id; | |
c13d2ab4 RL |
77 | |
78 | if (store == NULL | |
79 | && (store = get_default_method_store(libctx)) == NULL) | |
80 | return NULL; | |
81 | ||
2e49c054 RL |
82 | if ((namemap = ossl_namemap_stored(libctx)) == NULL |
83 | || (id = ossl_namemap_add(namemap, name)) == 0) | |
84 | return NULL; | |
85 | ||
86 | (void)ossl_method_store_fetch(store, id, propquery, &method); | |
c13d2ab4 RL |
87 | |
88 | if (method != NULL | |
89 | && !methdata->refcnt_up_method(method)) { | |
90 | method = NULL; | |
91 | } | |
92 | return method; | |
93 | } | |
94 | ||
95 | static int put_method_in_store(OPENSSL_CTX *libctx, void *store, | |
2e49c054 RL |
96 | void *method, const char *name, |
97 | const char *propdef, void *data) | |
c13d2ab4 RL |
98 | { |
99 | struct method_data_st *methdata = data; | |
2e49c054 RL |
100 | OSSL_NAMEMAP *namemap; |
101 | int id; | |
dc46e3dd | 102 | |
2e49c054 RL |
103 | if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL |
104 | || (id = ossl_namemap_add(namemap, name)) == 0) | |
dc46e3dd | 105 | return 0; |
c13d2ab4 RL |
106 | |
107 | if (store == NULL | |
108 | && (store = get_default_method_store(libctx)) == NULL) | |
109 | return 0; | |
110 | ||
111 | if (methdata->refcnt_up_method(method) | |
2e49c054 | 112 | && ossl_method_store_add(store, id, propdef, method, |
c13d2ab4 RL |
113 | methdata->destruct_method)) |
114 | return 1; | |
115 | return 0; | |
116 | } | |
117 | ||
2e49c054 RL |
118 | static void *construct_method(const char *name, const OSSL_DISPATCH *fns, |
119 | OSSL_PROVIDER *prov, void *data) | |
c13d2ab4 RL |
120 | { |
121 | struct method_data_st *methdata = data; | |
c13d2ab4 | 122 | |
0211740f | 123 | return methdata->method_from_dispatch(fns, prov); |
c13d2ab4 RL |
124 | } |
125 | ||
126 | static void destruct_method(void *method, void *data) | |
127 | { | |
128 | struct method_data_st *methdata = data; | |
129 | ||
130 | methdata->destruct_method(method); | |
131 | } | |
132 | ||
133 | void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id, | |
2e49c054 | 134 | const char *name, const char *properties, |
0211740f | 135 | void *(*new_method)(const OSSL_DISPATCH *fns, |
c13d2ab4 RL |
136 | OSSL_PROVIDER *prov), |
137 | int (*upref_method)(void *), | |
0211740f | 138 | void (*free_method)(void *)) |
c13d2ab4 | 139 | { |
e019da7b | 140 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); |
baff732d | 141 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
2e49c054 | 142 | int id; |
c13d2ab4 RL |
143 | void *method = NULL; |
144 | ||
baff732d | 145 | if (store == NULL || namemap == NULL) |
e019da7b RL |
146 | return NULL; |
147 | ||
2e49c054 RL |
148 | if ((id = ossl_namemap_number(namemap, name)) == 0 |
149 | || !ossl_method_store_cache_get(store, id, properties, &method)) { | |
c13d2ab4 RL |
150 | OSSL_METHOD_CONSTRUCT_METHOD mcm = { |
151 | alloc_tmp_method_store, | |
152 | dealloc_tmp_method_store, | |
153 | get_method_from_store, | |
154 | put_method_in_store, | |
155 | construct_method, | |
156 | destruct_method | |
157 | }; | |
158 | struct method_data_st mcmdata; | |
159 | ||
c13d2ab4 | 160 | mcmdata.mcm = &mcm; |
baff732d | 161 | mcmdata.libctx = libctx; |
c13d2ab4 RL |
162 | mcmdata.method_from_dispatch = new_method; |
163 | mcmdata.destruct_method = free_method; | |
164 | mcmdata.refcnt_up_method = upref_method; | |
165 | mcmdata.destruct_method = free_method; | |
2e49c054 | 166 | method = ossl_method_construct(libctx, operation_id, name, |
c13d2ab4 RL |
167 | properties, 0 /* !force_cache */, |
168 | &mcm, &mcmdata); | |
2e49c054 | 169 | ossl_method_store_cache_set(store, id, properties, method); |
e019da7b RL |
170 | } else { |
171 | upref_method(method); | |
c13d2ab4 RL |
172 | } |
173 | ||
174 | return method; | |
175 | } | |
cb929645 RL |
176 | |
177 | int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq) | |
178 | { | |
179 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); | |
180 | ||
181 | if (store != NULL) | |
182 | return ossl_method_store_set_global_properties(store, propq); | |
183 | EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR); | |
184 | return 0; | |
185 | } |