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