Coverage Report

Created: 2025-06-13 06:56

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