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