Coverage Report

Created: 2023-06-08 06:43

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