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