/src/openssl30/crypto/encode_decode/encoder_meth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2022 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 <openssl/core.h> |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/encoder.h> |
13 | | #include <openssl/ui.h> |
14 | | #include "internal/core.h" |
15 | | #include "internal/namemap.h" |
16 | | #include "internal/property.h" |
17 | | #include "internal/provider.h" |
18 | | #include "crypto/encoder.h" |
19 | | #include "encoder_local.h" |
20 | | |
21 | | /* |
22 | | * Encoder can have multiple names, separated with colons in a name string |
23 | | */ |
24 | 3.05k | #define NAME_SEPARATOR ':' |
25 | | |
26 | | /* Simple method structure constructor and destructor */ |
27 | | static OSSL_ENCODER *ossl_encoder_new(void) |
28 | 460 | { |
29 | 460 | OSSL_ENCODER *encoder = NULL; |
30 | | |
31 | 460 | if ((encoder = OPENSSL_zalloc(sizeof(*encoder))) == NULL |
32 | 460 | || (encoder->base.lock = CRYPTO_THREAD_lock_new()) == NULL) { |
33 | 0 | OSSL_ENCODER_free(encoder); |
34 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | | |
38 | 460 | encoder->base.refcnt = 1; |
39 | | |
40 | 460 | return encoder; |
41 | 460 | } |
42 | | |
43 | | int OSSL_ENCODER_up_ref(OSSL_ENCODER *encoder) |
44 | 354k | { |
45 | 354k | int ref = 0; |
46 | | |
47 | 354k | CRYPTO_UP_REF(&encoder->base.refcnt, &ref, encoder->base.lock); |
48 | 354k | return 1; |
49 | 354k | } |
50 | | |
51 | | void OSSL_ENCODER_free(OSSL_ENCODER *encoder) |
52 | 355k | { |
53 | 355k | int ref = 0; |
54 | | |
55 | 355k | if (encoder == NULL) |
56 | 0 | return; |
57 | | |
58 | 355k | CRYPTO_DOWN_REF(&encoder->base.refcnt, &ref, encoder->base.lock); |
59 | 355k | if (ref > 0) |
60 | 354k | return; |
61 | 1.52k | OPENSSL_free(encoder->base.name); |
62 | 1.52k | ossl_property_free(encoder->base.parsed_propdef); |
63 | 1.52k | ossl_provider_free(encoder->base.prov); |
64 | 1.52k | CRYPTO_THREAD_lock_free(encoder->base.lock); |
65 | 1.52k | OPENSSL_free(encoder); |
66 | 1.52k | } |
67 | | |
68 | | /* Permanent encoder method store, constructor and destructor */ |
69 | | static void encoder_store_free(void *vstore) |
70 | 10 | { |
71 | 10 | ossl_method_store_free(vstore); |
72 | 10 | } |
73 | | |
74 | | static void *encoder_store_new(OSSL_LIB_CTX *ctx) |
75 | 10 | { |
76 | 10 | return ossl_method_store_new(ctx); |
77 | 10 | } |
78 | | |
79 | | |
80 | | static const OSSL_LIB_CTX_METHOD encoder_store_method = { |
81 | | /* We want encoder_store to be cleaned up before the provider store */ |
82 | | OSSL_LIB_CTX_METHOD_PRIORITY_2, |
83 | | encoder_store_new, |
84 | | encoder_store_free, |
85 | | }; |
86 | | |
87 | | /* Data to be passed through ossl_method_construct() */ |
88 | | struct encoder_data_st { |
89 | | OSSL_LIB_CTX *libctx; |
90 | | int id; /* For get_encoder_from_store() */ |
91 | | const char *names; /* For get_encoder_from_store() */ |
92 | | const char *propquery; /* For get_encoder_from_store() */ |
93 | | |
94 | | OSSL_METHOD_STORE *tmp_store; /* For get_tmp_encoder_store() */ |
95 | | |
96 | | unsigned int flag_construct_error_occurred : 1; |
97 | | }; |
98 | | |
99 | | /* |
100 | | * Generic routines to fetch / create ENCODER methods with |
101 | | * ossl_method_construct() |
102 | | */ |
103 | | |
104 | | /* Temporary encoder method store, constructor and destructor */ |
105 | | static void *get_tmp_encoder_store(void *data) |
106 | 0 | { |
107 | 0 | struct encoder_data_st *methdata = data; |
108 | |
|
109 | 0 | if (methdata->tmp_store == NULL) |
110 | 0 | methdata->tmp_store = ossl_method_store_new(methdata->libctx); |
111 | 0 | return methdata->tmp_store; |
112 | 0 | } |
113 | | |
114 | | static void dealloc_tmp_encoder_store(void *store) |
115 | 87.5k | { |
116 | 87.5k | if (store != NULL) |
117 | 0 | ossl_method_store_free(store); |
118 | 87.5k | } |
119 | | |
120 | | /* Get the permanent encoder store */ |
121 | | static OSSL_METHOD_STORE *get_encoder_store(OSSL_LIB_CTX *libctx) |
122 | 526k | { |
123 | 526k | return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX, |
124 | 526k | &encoder_store_method); |
125 | 526k | } |
126 | | |
127 | | static int reserve_encoder_store(void *store, void *data) |
128 | 175k | { |
129 | 175k | struct encoder_data_st *methdata = data; |
130 | | |
131 | 175k | if (store == NULL |
132 | 175k | && (store = get_encoder_store(methdata->libctx)) == NULL) |
133 | 0 | return 0; |
134 | | |
135 | 175k | return ossl_method_lock_store(store); |
136 | 175k | } |
137 | | |
138 | | static int unreserve_encoder_store(void *store, void *data) |
139 | 175k | { |
140 | 175k | struct encoder_data_st *methdata = data; |
141 | | |
142 | 175k | if (store == NULL |
143 | 175k | && (store = get_encoder_store(methdata->libctx)) == NULL) |
144 | 0 | return 0; |
145 | | |
146 | 175k | return ossl_method_unlock_store(store); |
147 | 175k | } |
148 | | |
149 | | /* Get encoder methods from a store, or put one in */ |
150 | | static void *get_encoder_from_store(void *store, const OSSL_PROVIDER **prov, |
151 | | void *data) |
152 | 87.5k | { |
153 | 87.5k | struct encoder_data_st *methdata = data; |
154 | 87.5k | void *method = NULL; |
155 | 87.5k | int id; |
156 | | |
157 | | /* |
158 | | * get_encoder_from_store() is only called to try and get the method |
159 | | * that OSSL_ENCODER_fetch() is asking for, and the name or name id are |
160 | | * passed via methdata. |
161 | | */ |
162 | 87.5k | if ((id = methdata->id) == 0 && methdata->names != NULL) { |
163 | 0 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx); |
164 | 0 | const char *names = methdata->names; |
165 | 0 | const char *q = strchr(names, NAME_SEPARATOR); |
166 | 0 | size_t l = (q == NULL ? strlen(names) : (size_t)(q - names)); |
167 | |
|
168 | 0 | if (namemap == 0) |
169 | 0 | return NULL; |
170 | 0 | id = ossl_namemap_name2num_n(namemap, methdata->names, l); |
171 | 0 | } |
172 | | |
173 | 87.5k | if (id == 0) |
174 | 87.5k | return NULL; |
175 | | |
176 | 0 | if (store == NULL |
177 | 0 | && (store = get_encoder_store(methdata->libctx)) == NULL) |
178 | 0 | return NULL; |
179 | | |
180 | 0 | if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method)) |
181 | 0 | return NULL; |
182 | 0 | return method; |
183 | 0 | } |
184 | | |
185 | | static int put_encoder_in_store(void *store, void *method, |
186 | | const OSSL_PROVIDER *prov, |
187 | | const char *names, const char *propdef, |
188 | | void *data) |
189 | 1.52k | { |
190 | 1.52k | struct encoder_data_st *methdata = data; |
191 | 1.52k | OSSL_NAMEMAP *namemap; |
192 | 1.52k | int id; |
193 | 1.52k | size_t l = 0; |
194 | | |
195 | | /* |
196 | | * put_encoder_in_store() is only called with an OSSL_ENCODER method that |
197 | | * was successfully created by construct_encoder() below, which means that |
198 | | * all the names should already be stored in the namemap with the same |
199 | | * numeric identity, so just use the first to get that identity. |
200 | | */ |
201 | 1.52k | if (names != NULL) { |
202 | 1.52k | const char *q = strchr(names, NAME_SEPARATOR); |
203 | | |
204 | 1.52k | l = (q == NULL ? strlen(names) : (size_t)(q - names)); |
205 | 1.52k | } |
206 | | |
207 | 1.52k | if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL |
208 | 1.52k | || (id = ossl_namemap_name2num_n(namemap, names, l)) == 0) |
209 | 0 | return 0; |
210 | | |
211 | 1.52k | if (store == NULL && (store = get_encoder_store(methdata->libctx)) == NULL) |
212 | 0 | return 0; |
213 | | |
214 | 1.52k | return ossl_method_store_add(store, prov, id, propdef, method, |
215 | 1.52k | (int (*)(void *))OSSL_ENCODER_up_ref, |
216 | 1.52k | (void (*)(void *))OSSL_ENCODER_free); |
217 | 1.52k | } |
218 | | |
219 | | /* Create and populate a encoder method */ |
220 | | static void *encoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef, |
221 | | OSSL_PROVIDER *prov) |
222 | 1.52k | { |
223 | 1.52k | OSSL_ENCODER *encoder = NULL; |
224 | 1.52k | const OSSL_DISPATCH *fns = algodef->implementation; |
225 | 1.52k | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
226 | | |
227 | 1.52k | if ((encoder = ossl_encoder_new()) == NULL) |
228 | 0 | return NULL; |
229 | 1.52k | encoder->base.id = id; |
230 | 1.52k | if ((encoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) { |
231 | 0 | OSSL_ENCODER_free(encoder); |
232 | 0 | return NULL; |
233 | 0 | } |
234 | 1.52k | encoder->base.algodef = algodef; |
235 | 1.52k | if ((encoder->base.parsed_propdef |
236 | 1.52k | = ossl_parse_property(libctx, algodef->property_definition)) == NULL) { |
237 | 0 | OSSL_ENCODER_free(encoder); |
238 | 0 | return NULL; |
239 | 0 | } |
240 | | |
241 | 13.1k | for (; fns->function_id != 0; fns++) { |
242 | 11.6k | switch (fns->function_id) { |
243 | 1.52k | case OSSL_FUNC_ENCODER_NEWCTX: |
244 | 1.52k | if (encoder->newctx == NULL) |
245 | 1.52k | encoder->newctx = |
246 | 1.52k | OSSL_FUNC_encoder_newctx(fns); |
247 | 1.52k | break; |
248 | 1.52k | case OSSL_FUNC_ENCODER_FREECTX: |
249 | 1.52k | if (encoder->freectx == NULL) |
250 | 1.52k | encoder->freectx = |
251 | 1.52k | OSSL_FUNC_encoder_freectx(fns); |
252 | 1.52k | break; |
253 | 0 | case OSSL_FUNC_ENCODER_GET_PARAMS: |
254 | 0 | if (encoder->get_params == NULL) |
255 | 0 | encoder->get_params = |
256 | 0 | OSSL_FUNC_encoder_get_params(fns); |
257 | 0 | break; |
258 | 0 | case OSSL_FUNC_ENCODER_GETTABLE_PARAMS: |
259 | 0 | if (encoder->gettable_params == NULL) |
260 | 0 | encoder->gettable_params = |
261 | 0 | OSSL_FUNC_encoder_gettable_params(fns); |
262 | 0 | break; |
263 | 1.32k | case OSSL_FUNC_ENCODER_SET_CTX_PARAMS: |
264 | 1.32k | if (encoder->set_ctx_params == NULL) |
265 | 1.32k | encoder->set_ctx_params = |
266 | 1.32k | OSSL_FUNC_encoder_set_ctx_params(fns); |
267 | 1.32k | break; |
268 | 1.32k | case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS: |
269 | 1.32k | if (encoder->settable_ctx_params == NULL) |
270 | 1.32k | encoder->settable_ctx_params = |
271 | 1.32k | OSSL_FUNC_encoder_settable_ctx_params(fns); |
272 | 1.32k | break; |
273 | 1.36k | case OSSL_FUNC_ENCODER_DOES_SELECTION: |
274 | 1.36k | if (encoder->does_selection == NULL) |
275 | 1.36k | encoder->does_selection = |
276 | 1.36k | OSSL_FUNC_encoder_does_selection(fns); |
277 | 1.36k | break; |
278 | 1.52k | case OSSL_FUNC_ENCODER_ENCODE: |
279 | 1.52k | if (encoder->encode == NULL) |
280 | 1.52k | encoder->encode = OSSL_FUNC_encoder_encode(fns); |
281 | 1.52k | break; |
282 | 1.52k | case OSSL_FUNC_ENCODER_IMPORT_OBJECT: |
283 | 1.52k | if (encoder->import_object == NULL) |
284 | 1.52k | encoder->import_object = |
285 | 1.52k | OSSL_FUNC_encoder_import_object(fns); |
286 | 1.52k | break; |
287 | 1.52k | case OSSL_FUNC_ENCODER_FREE_OBJECT: |
288 | 1.52k | if (encoder->free_object == NULL) |
289 | 1.52k | encoder->free_object = |
290 | 1.52k | OSSL_FUNC_encoder_free_object(fns); |
291 | 1.52k | break; |
292 | 11.6k | } |
293 | 11.6k | } |
294 | | /* |
295 | | * Try to check that the method is sensible. |
296 | | * If you have a constructor, you must have a destructor and vice versa. |
297 | | * You must have the encoding driver functions. |
298 | | */ |
299 | 1.52k | if (!((encoder->newctx == NULL && encoder->freectx == NULL) |
300 | 1.52k | || (encoder->newctx != NULL && encoder->freectx != NULL) |
301 | 1.52k | || (encoder->import_object != NULL && encoder->free_object != NULL) |
302 | 1.52k | || (encoder->import_object == NULL && encoder->free_object == NULL)) |
303 | 1.52k | || encoder->encode == NULL) { |
304 | 0 | OSSL_ENCODER_free(encoder); |
305 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INVALID_PROVIDER_FUNCTIONS); |
306 | 0 | return NULL; |
307 | 0 | } |
308 | | |
309 | 1.52k | if (prov != NULL && !ossl_provider_up_ref(prov)) { |
310 | 0 | OSSL_ENCODER_free(encoder); |
311 | 0 | return NULL; |
312 | 0 | } |
313 | | |
314 | 1.52k | encoder->base.prov = prov; |
315 | 1.52k | return encoder; |
316 | 1.52k | } |
317 | | |
318 | | |
319 | | /* |
320 | | * The core fetching functionality passes the names of the implementation. |
321 | | * This function is responsible to getting an identity number for them, |
322 | | * then call encoder_from_algorithm() with that identity number. |
323 | | */ |
324 | | static void *construct_encoder(const OSSL_ALGORITHM *algodef, |
325 | | OSSL_PROVIDER *prov, void *data) |
326 | 1.52k | { |
327 | | /* |
328 | | * This function is only called if get_encoder_from_store() returned |
329 | | * NULL, so it's safe to say that of all the spots to create a new |
330 | | * namemap entry, this is it. Should the name already exist there, we |
331 | | * know that ossl_namemap_add() will return its corresponding number. |
332 | | */ |
333 | 1.52k | struct encoder_data_st *methdata = data; |
334 | 1.52k | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
335 | 1.52k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
336 | 1.52k | const char *names = algodef->algorithm_names; |
337 | 1.52k | int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR); |
338 | 1.52k | void *method = NULL; |
339 | | |
340 | 1.52k | if (id != 0) |
341 | 1.52k | method = encoder_from_algorithm(id, algodef, prov); |
342 | | |
343 | | /* |
344 | | * Flag to indicate that there was actual construction errors. This |
345 | | * helps inner_evp_generic_fetch() determine what error it should |
346 | | * record on inaccessible algorithms. |
347 | | */ |
348 | 1.52k | if (method == NULL) |
349 | 0 | methdata->flag_construct_error_occurred = 1; |
350 | | |
351 | 1.52k | return method; |
352 | 1.52k | } |
353 | | |
354 | | /* Intermediary function to avoid ugly casts, used below */ |
355 | | static void destruct_encoder(void *method, void *data) |
356 | 1.52k | { |
357 | 1.52k | OSSL_ENCODER_free(method); |
358 | 1.52k | } |
359 | | |
360 | | static int up_ref_encoder(void *method) |
361 | 0 | { |
362 | 0 | return OSSL_ENCODER_up_ref(method); |
363 | 0 | } |
364 | | |
365 | | static void free_encoder(void *method) |
366 | 0 | { |
367 | 0 | OSSL_ENCODER_free(method); |
368 | 0 | } |
369 | | |
370 | | /* Fetching support. Can fetch by numeric identity or by name */ |
371 | | static OSSL_ENCODER * |
372 | | inner_ossl_encoder_fetch(struct encoder_data_st *methdata, int id, |
373 | | const char *name, const char *properties) |
374 | 8.56k | { |
375 | 8.56k | OSSL_METHOD_STORE *store = get_encoder_store(methdata->libctx); |
376 | 8.56k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx); |
377 | 8.56k | const char *const propq = properties != NULL ? properties : ""; |
378 | 8.56k | void *method = NULL; |
379 | 8.56k | int unsupported = 0; |
380 | | |
381 | 8.56k | if (store == NULL || namemap == NULL) { |
382 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT); |
383 | 0 | return NULL; |
384 | 0 | } |
385 | | |
386 | | /* |
387 | | * If we have been passed both an id and a name, we have an |
388 | | * internal programming error. |
389 | | */ |
390 | 8.56k | if (!ossl_assert(id == 0 || name == NULL)) { |
391 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR); |
392 | 0 | return NULL; |
393 | 0 | } |
394 | | |
395 | 8.56k | if (id == 0) |
396 | 8.56k | id = ossl_namemap_name2num(namemap, name); |
397 | | |
398 | | /* |
399 | | * If we haven't found the name yet, chances are that the algorithm to |
400 | | * be fetched is unsupported. |
401 | | */ |
402 | 8.56k | if (id == 0) |
403 | 8.56k | unsupported = 1; |
404 | | |
405 | 8.56k | if (id == 0 |
406 | 8.56k | || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) { |
407 | 8.56k | OSSL_METHOD_CONSTRUCT_METHOD mcm = { |
408 | 8.56k | get_tmp_encoder_store, |
409 | 8.56k | reserve_encoder_store, |
410 | 8.56k | unreserve_encoder_store, |
411 | 8.56k | get_encoder_from_store, |
412 | 8.56k | put_encoder_in_store, |
413 | 8.56k | construct_encoder, |
414 | 8.56k | destruct_encoder |
415 | 8.56k | }; |
416 | 8.56k | OSSL_PROVIDER *prov = NULL; |
417 | | |
418 | 8.56k | methdata->id = id; |
419 | 8.56k | methdata->names = name; |
420 | 8.56k | methdata->propquery = propq; |
421 | 8.56k | methdata->flag_construct_error_occurred = 0; |
422 | 8.56k | if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_ENCODER, |
423 | 8.56k | &prov, 0 /* !force_cache */, |
424 | 8.56k | &mcm, methdata)) != NULL) { |
425 | | /* |
426 | | * If construction did create a method for us, we know that |
427 | | * there is a correct name_id and meth_id, since those have |
428 | | * already been calculated in get_encoder_from_store() and |
429 | | * put_encoder_in_store() above. |
430 | | */ |
431 | 0 | if (id == 0) |
432 | 0 | id = ossl_namemap_name2num(namemap, name); |
433 | 0 | ossl_method_store_cache_set(store, prov, id, propq, method, |
434 | 0 | up_ref_encoder, free_encoder); |
435 | 0 | } |
436 | | |
437 | | /* |
438 | | * If we never were in the constructor, the algorithm to be fetched |
439 | | * is unsupported. |
440 | | */ |
441 | 8.56k | unsupported = !methdata->flag_construct_error_occurred; |
442 | 8.56k | } |
443 | | |
444 | 8.56k | if ((id != 0 || name != NULL) && method == NULL) { |
445 | 0 | int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED; |
446 | |
|
447 | 0 | if (name == NULL) |
448 | 0 | name = ossl_namemap_num2name(namemap, id, 0); |
449 | 0 | ERR_raise_data(ERR_LIB_OSSL_ENCODER, code, |
450 | 0 | "%s, Name (%s : %d), Properties (%s)", |
451 | 0 | ossl_lib_ctx_get_descriptor(methdata->libctx), |
452 | 0 | name == NULL ? "<null>" : name, id, |
453 | 0 | properties == NULL ? "<null>" : properties); |
454 | 0 | } |
455 | | |
456 | 8.56k | return method; |
457 | 8.56k | } |
458 | | |
459 | | OSSL_ENCODER *OSSL_ENCODER_fetch(OSSL_LIB_CTX *libctx, const char *name, |
460 | | const char *properties) |
461 | 0 | { |
462 | 0 | struct encoder_data_st methdata; |
463 | 0 | void *method; |
464 | |
|
465 | 0 | methdata.libctx = libctx; |
466 | 0 | methdata.tmp_store = NULL; |
467 | 0 | method = inner_ossl_encoder_fetch(&methdata, 0, name, properties); |
468 | 0 | dealloc_tmp_encoder_store(methdata.tmp_store); |
469 | 0 | return method; |
470 | 0 | } |
471 | | |
472 | | OSSL_ENCODER *ossl_encoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id, |
473 | | const char *properties) |
474 | 0 | { |
475 | 0 | struct encoder_data_st methdata; |
476 | 0 | void *method; |
477 | |
|
478 | 0 | methdata.libctx = libctx; |
479 | 0 | methdata.tmp_store = NULL; |
480 | 0 | method = inner_ossl_encoder_fetch(&methdata, id, NULL, properties); |
481 | 0 | dealloc_tmp_encoder_store(methdata.tmp_store); |
482 | 0 | return method; |
483 | 0 | } |
484 | | |
485 | | int ossl_encoder_store_cache_flush(OSSL_LIB_CTX *libctx) |
486 | 60 | { |
487 | 60 | OSSL_METHOD_STORE *store = get_encoder_store(libctx); |
488 | | |
489 | 60 | if (store != NULL) |
490 | 60 | return ossl_method_store_cache_flush_all(store); |
491 | 0 | return 1; |
492 | 60 | } |
493 | | |
494 | | int ossl_encoder_store_remove_all_provided(const OSSL_PROVIDER *prov) |
495 | 0 | { |
496 | 0 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
497 | 0 | OSSL_METHOD_STORE *store = get_encoder_store(libctx); |
498 | |
|
499 | 0 | if (store != NULL) |
500 | 0 | return ossl_method_store_remove_all_provided(store, prov); |
501 | 0 | return 1; |
502 | 0 | } |
503 | | |
504 | | /* |
505 | | * Library of basic method functions |
506 | | */ |
507 | | |
508 | | const OSSL_PROVIDER *OSSL_ENCODER_get0_provider(const OSSL_ENCODER *encoder) |
509 | 19.4M | { |
510 | 19.4M | if (!ossl_assert(encoder != NULL)) { |
511 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
512 | 0 | return 0; |
513 | 0 | } |
514 | | |
515 | 19.4M | return encoder->base.prov; |
516 | 19.4M | } |
517 | | |
518 | | const char *OSSL_ENCODER_get0_properties(const OSSL_ENCODER *encoder) |
519 | 0 | { |
520 | 0 | if (!ossl_assert(encoder != NULL)) { |
521 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
522 | 0 | return 0; |
523 | 0 | } |
524 | | |
525 | 0 | return encoder->base.algodef->property_definition; |
526 | 0 | } |
527 | | |
528 | | const OSSL_PROPERTY_LIST * |
529 | | ossl_encoder_parsed_properties(const OSSL_ENCODER *encoder) |
530 | 352k | { |
531 | 352k | if (!ossl_assert(encoder != NULL)) { |
532 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
533 | 0 | return 0; |
534 | 0 | } |
535 | | |
536 | 352k | return encoder->base.parsed_propdef; |
537 | 352k | } |
538 | | |
539 | | int ossl_encoder_get_number(const OSSL_ENCODER *encoder) |
540 | 0 | { |
541 | 0 | if (!ossl_assert(encoder != NULL)) { |
542 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
543 | 0 | return 0; |
544 | 0 | } |
545 | | |
546 | 0 | return encoder->base.id; |
547 | 0 | } |
548 | | |
549 | | const char *OSSL_ENCODER_get0_name(const OSSL_ENCODER *encoder) |
550 | 43.6k | { |
551 | 43.6k | return encoder->base.name; |
552 | 43.6k | } |
553 | | |
554 | | const char *OSSL_ENCODER_get0_description(const OSSL_ENCODER *encoder) |
555 | 0 | { |
556 | 0 | return encoder->base.algodef->algorithm_description; |
557 | 0 | } |
558 | | |
559 | | int OSSL_ENCODER_is_a(const OSSL_ENCODER *encoder, const char *name) |
560 | 2.65M | { |
561 | 2.65M | if (encoder->base.prov != NULL) { |
562 | 2.65M | OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov); |
563 | 2.65M | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
564 | | |
565 | 2.65M | return ossl_namemap_name2num(namemap, name) == encoder->base.id; |
566 | 2.65M | } |
567 | 0 | return 0; |
568 | 2.65M | } |
569 | | |
570 | | struct do_one_data_st { |
571 | | void (*user_fn)(OSSL_ENCODER *encoder, void *arg); |
572 | | void *user_arg; |
573 | | }; |
574 | | |
575 | | static void do_one(ossl_unused int id, void *method, void *arg) |
576 | 14.1M | { |
577 | 14.1M | struct do_one_data_st *data = arg; |
578 | | |
579 | 14.1M | data->user_fn(method, data->user_arg); |
580 | 14.1M | } |
581 | | |
582 | | void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX *libctx, |
583 | | void (*user_fn)(OSSL_ENCODER *encoder, |
584 | | void *arg), |
585 | | void *user_arg) |
586 | 87.5k | { |
587 | 87.5k | struct encoder_data_st methdata; |
588 | 87.5k | struct do_one_data_st data; |
589 | | |
590 | 87.5k | methdata.libctx = libctx; |
591 | 87.5k | methdata.tmp_store = NULL; |
592 | 87.5k | (void)inner_ossl_encoder_fetch(&methdata, 0, NULL, NULL /* properties */); |
593 | | |
594 | 87.5k | data.user_fn = user_fn; |
595 | 87.5k | data.user_arg = user_arg; |
596 | 87.5k | if (methdata.tmp_store != NULL) |
597 | 0 | ossl_method_store_do_all(methdata.tmp_store, &do_one, &data); |
598 | 87.5k | ossl_method_store_do_all(get_encoder_store(libctx), &do_one, &data); |
599 | 87.5k | dealloc_tmp_encoder_store(methdata.tmp_store); |
600 | 87.5k | } |
601 | | |
602 | | int OSSL_ENCODER_names_do_all(const OSSL_ENCODER *encoder, |
603 | | void (*fn)(const char *name, void *data), |
604 | | void *data) |
605 | 0 | { |
606 | 0 | if (encoder == NULL) |
607 | 0 | return 0; |
608 | | |
609 | 0 | if (encoder->base.prov != NULL) { |
610 | 0 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov); |
611 | 0 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
612 | |
|
613 | 0 | return ossl_namemap_doall_names(namemap, encoder->base.id, fn, data); |
614 | 0 | } |
615 | | |
616 | 0 | return 1; |
617 | 0 | } |
618 | | |
619 | | const OSSL_PARAM * |
620 | | OSSL_ENCODER_gettable_params(OSSL_ENCODER *encoder) |
621 | 0 | { |
622 | 0 | if (encoder != NULL && encoder->gettable_params != NULL) { |
623 | 0 | void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder)); |
624 | |
|
625 | 0 | return encoder->gettable_params(provctx); |
626 | 0 | } |
627 | 0 | return NULL; |
628 | 0 | } |
629 | | |
630 | | int OSSL_ENCODER_get_params(OSSL_ENCODER *encoder, OSSL_PARAM params[]) |
631 | 0 | { |
632 | 0 | if (encoder != NULL && encoder->get_params != NULL) |
633 | 0 | return encoder->get_params(params); |
634 | 0 | return 0; |
635 | 0 | } |
636 | | |
637 | | const OSSL_PARAM *OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER *encoder) |
638 | 0 | { |
639 | 0 | if (encoder != NULL && encoder->settable_ctx_params != NULL) { |
640 | 0 | void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder)); |
641 | |
|
642 | 0 | return encoder->settable_ctx_params(provctx); |
643 | 0 | } |
644 | 0 | return NULL; |
645 | 0 | } |
646 | | |
647 | | /* |
648 | | * Encoder context support |
649 | | */ |
650 | | |
651 | | OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new(void) |
652 | 9.88k | { |
653 | 9.88k | OSSL_ENCODER_CTX *ctx; |
654 | | |
655 | 9.88k | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) |
656 | 9.88k | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE); |
657 | | |
658 | 9.88k | return ctx; |
659 | 9.88k | } |
660 | | |
661 | | int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX *ctx, |
662 | | const OSSL_PARAM params[]) |
663 | 44.6k | { |
664 | 44.6k | int ok = 1; |
665 | 44.6k | size_t i; |
666 | 44.6k | size_t l; |
667 | | |
668 | 44.6k | if (!ossl_assert(ctx != NULL)) { |
669 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
670 | 0 | return 0; |
671 | 0 | } |
672 | | |
673 | 44.6k | if (ctx->encoder_insts == NULL) |
674 | 874 | return 1; |
675 | | |
676 | 43.7k | l = OSSL_ENCODER_CTX_get_num_encoders(ctx); |
677 | 396k | for (i = 0; i < l; i++) { |
678 | 352k | OSSL_ENCODER_INSTANCE *encoder_inst = |
679 | 352k | sk_OSSL_ENCODER_INSTANCE_value(ctx->encoder_insts, i); |
680 | 352k | OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst); |
681 | 352k | void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst); |
682 | | |
683 | 352k | if (encoderctx == NULL || encoder->set_ctx_params == NULL) |
684 | 63.1k | continue; |
685 | 289k | if (!encoder->set_ctx_params(encoderctx, params)) |
686 | 0 | ok = 0; |
687 | 289k | } |
688 | 43.7k | return ok; |
689 | 44.6k | } |
690 | | |
691 | | void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX *ctx) |
692 | 44.6k | { |
693 | 44.6k | if (ctx != NULL) { |
694 | 44.6k | sk_OSSL_ENCODER_INSTANCE_pop_free(ctx->encoder_insts, |
695 | 44.6k | ossl_encoder_instance_free); |
696 | 44.6k | OPENSSL_free(ctx->construct_data); |
697 | 44.6k | ossl_pw_clear_passphrase_data(&ctx->pwdata); |
698 | 44.6k | OPENSSL_free(ctx); |
699 | 44.6k | } |
700 | 44.6k | } |