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