Coverage Report

Created: 2026-07-24 06:26

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