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