Coverage Report

Created: 2026-07-16 06:59

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