Coverage Report

Created: 2025-06-13 06:58

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