Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/encode_decode/encoder_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2024 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_names.h>
11
#include <openssl/bio.h>
12
#include <openssl/encoder.h>
13
#include <openssl/buffer.h>
14
#include <openssl/params.h>
15
#include <openssl/provider.h>
16
#include <openssl/trace.h>
17
#include "internal/bio.h"
18
#include "internal/provider.h"
19
#include "encoder_local.h"
20
21
struct encoder_process_data_st {
22
    OSSL_ENCODER_CTX *ctx;
23
24
    /* Current BIO */
25
    BIO *bio;
26
27
    /* Index of the current encoder instance to be processed */
28
    int current_encoder_inst_index;
29
30
    /* Processing data passed down through recursion */
31
    int level; /* Recursion level */
32
    OSSL_ENCODER_INSTANCE *next_encoder_inst;
33
    int count_output_structure;
34
35
    /* Processing data passed up through recursion */
36
    OSSL_ENCODER_INSTANCE *prev_encoder_inst;
37
    unsigned char *running_output;
38
    size_t running_output_length;
39
    /* Data type = the name of the first succeeding encoder implementation */
40
    const char *data_type;
41
};
42
43
static int encoder_process(struct encoder_process_data_st *data);
44
45
int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
46
91.8k
{
47
91.8k
    struct encoder_process_data_st data;
48
49
91.8k
    memset(&data, 0, sizeof(data));
50
91.8k
    data.ctx = ctx;
51
91.8k
    data.bio = out;
52
91.8k
    data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
53
54
91.8k
    if (data.current_encoder_inst_index == 0) {
55
0
        ERR_raise_data(ERR_LIB_OSSL_ENCODER, OSSL_ENCODER_R_ENCODER_NOT_FOUND,
56
0
            "No encoders were found. For standard encoders you need "
57
0
            "at least one of the default or base providers "
58
0
            "available. Did you forget to load them?");
59
0
        return 0;
60
0
    }
61
62
91.8k
    if (ctx->cleanup == NULL || ctx->construct == NULL) {
63
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INIT_FAIL);
64
0
        return 0;
65
0
    }
66
67
91.8k
    return encoder_process(&data) > 0;
68
91.8k
}
69
70
#ifndef OPENSSL_NO_STDIO
71
static BIO *bio_from_file(FILE *fp)
72
0
{
73
0
    BIO *b;
74
75
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
76
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
77
0
        return NULL;
78
0
    }
79
0
    BIO_set_fp(b, fp, BIO_NOCLOSE);
80
0
    return b;
81
0
}
82
83
int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
84
0
{
85
0
    BIO *b = bio_from_file(fp);
86
0
    int ret = 0;
87
88
0
    if (b != NULL)
89
0
        ret = OSSL_ENCODER_to_bio(ctx, b);
90
91
0
    BIO_free(b);
92
0
    return ret;
93
0
}
94
#endif
95
96
int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
97
    size_t *pdata_len)
98
11.3k
{
99
11.3k
    BIO *out;
100
11.3k
    BUF_MEM *buf = NULL;
101
11.3k
    int ret = 0;
102
103
11.3k
    if (pdata_len == NULL) {
104
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
105
0
        return 0;
106
0
    }
107
108
11.3k
    out = BIO_new(BIO_s_mem());
109
110
11.3k
    if (out != NULL
111
11.3k
        && OSSL_ENCODER_to_bio(ctx, out)
112
8.15k
        && BIO_get_mem_ptr(out, &buf) > 0) {
113
8.15k
        ret = 1; /* Hope for the best. A too small buffer will clear this */
114
115
8.15k
        if (pdata != NULL && *pdata != NULL) {
116
0
            if (*pdata_len < buf->length)
117
                /*
118
                 * It's tempting to do |*pdata_len = (size_t)buf->length|
119
                 * However, it's believed to be confusing more than helpful,
120
                 * so we don't.
121
                 */
122
0
                ret = 0;
123
0
            else
124
0
                *pdata_len -= buf->length;
125
8.15k
        } else {
126
            /* The buffer with the right size is already allocated for us */
127
8.15k
            *pdata_len = (size_t)buf->length;
128
8.15k
        }
129
130
8.15k
        if (ret) {
131
8.15k
            if (pdata != NULL) {
132
8.15k
                if (*pdata != NULL) {
133
0
                    memcpy(*pdata, buf->data, buf->length);
134
0
                    *pdata += buf->length;
135
8.15k
                } else {
136
                    /* In this case, we steal the data from BIO_s_mem() */
137
8.15k
                    *pdata = (unsigned char *)buf->data;
138
8.15k
                    buf->data = NULL;
139
8.15k
                }
140
8.15k
            }
141
8.15k
        }
142
8.15k
    }
143
11.3k
    BIO_free(out);
144
11.3k
    return ret;
145
11.3k
}
146
147
int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
148
77.8k
{
149
77.8k
    if (!ossl_assert(ctx != NULL)) {
150
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
151
0
        return 0;
152
0
    }
153
154
77.8k
    if (!ossl_assert(selection != 0)) {
155
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
156
0
        return 0;
157
0
    }
158
159
77.8k
    ctx->selection = selection;
160
77.8k
    return 1;
161
77.8k
}
162
163
int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
164
    const char *output_type)
165
77.8k
{
166
77.8k
    if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
167
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
168
0
        return 0;
169
0
    }
170
171
77.8k
    ctx->output_type = output_type;
172
77.8k
    return 1;
173
77.8k
}
174
175
int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
176
    const char *output_structure)
177
9.55k
{
178
9.55k
    if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
179
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
180
0
        return 0;
181
0
    }
182
183
9.55k
    ctx->output_structure = output_structure;
184
9.55k
    return 1;
185
9.55k
}
186
187
static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
188
    void *encoderctx)
189
600k
{
190
600k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
191
600k
    const OSSL_PROVIDER *prov;
192
600k
    OSSL_LIB_CTX *libctx;
193
600k
    const OSSL_PROPERTY_LIST *props;
194
600k
    const OSSL_PROPERTY_DEFINITION *prop;
195
196
600k
    if (!ossl_assert(encoder != NULL)) {
197
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
198
0
        return 0;
199
0
    }
200
201
600k
    if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL) {
202
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
203
0
        return 0;
204
0
    }
205
206
600k
    if (!OSSL_ENCODER_up_ref(encoder)) {
207
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
208
0
        goto err;
209
0
    }
210
211
600k
    prov = OSSL_ENCODER_get0_provider(encoder);
212
600k
    libctx = ossl_provider_libctx(prov);
213
600k
    props = ossl_encoder_parsed_properties(encoder);
214
600k
    if (props == NULL) {
215
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
216
0
            "there are no property definitions with encoder %s",
217
0
            OSSL_ENCODER_get0_name(encoder));
218
0
        goto err;
219
0
    }
220
221
    /* The "output" property is mandatory */
222
600k
    prop = ossl_property_find_property(props, libctx, "output");
223
600k
    encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
224
600k
    if (encoder_inst->output_type == NULL) {
225
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
226
0
            "the mandatory 'output' property is missing "
227
0
            "for encoder %s (properties: %s)",
228
0
            OSSL_ENCODER_get0_name(encoder),
229
0
            OSSL_ENCODER_get0_properties(encoder));
230
0
        goto err;
231
0
    }
232
233
    /* The "structure" property is optional */
234
600k
    prop = ossl_property_find_property(props, libctx, "structure");
235
600k
    if (prop != NULL)
236
467k
        encoder_inst->output_structure
237
467k
            = ossl_property_get_string_value(libctx, prop);
238
239
600k
    encoder_inst->encoder = encoder;
240
600k
    encoder_inst->encoderctx = encoderctx;
241
600k
    return encoder_inst;
242
0
err:
243
0
    ossl_encoder_instance_free(encoder_inst);
244
0
    return NULL;
245
600k
}
246
247
void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
248
729k
{
249
729k
    if (encoder_inst != NULL) {
250
729k
        if (encoder_inst->encoder != NULL)
251
729k
            encoder_inst->encoder->freectx(encoder_inst->encoderctx);
252
729k
        encoder_inst->encoderctx = NULL;
253
729k
        OSSL_ENCODER_free(encoder_inst->encoder);
254
729k
        encoder_inst->encoder = NULL;
255
729k
        OPENSSL_free(encoder_inst);
256
729k
    }
257
729k
}
258
259
static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
260
    OSSL_ENCODER_INSTANCE *ei)
261
729k
{
262
729k
    int ok;
263
264
729k
    if (ctx->encoder_insts == NULL
265
91.8k
        && (ctx->encoder_insts = sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
266
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
267
0
        return 0;
268
0
    }
269
270
729k
    ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
271
729k
    if (ok) {
272
729k
        OSSL_TRACE_BEGIN(ENCODER)
273
0
        {
274
0
            BIO_printf(trc_out,
275
0
                "(ctx %p) Added encoder instance %p (encoder %p):\n"
276
0
                "    %s with %s\n",
277
0
                (void *)ctx, (void *)ei, (void *)ei->encoder,
278
0
                OSSL_ENCODER_get0_name(ei->encoder),
279
0
                OSSL_ENCODER_get0_properties(ei->encoder));
280
0
        }
281
729k
        OSSL_TRACE_END(ENCODER);
282
729k
    }
283
729k
    return ok;
284
729k
}
285
286
int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
287
600k
{
288
600k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
289
600k
    const OSSL_PROVIDER *prov = NULL;
290
600k
    void *encoderctx = NULL;
291
600k
    void *provctx = NULL;
292
293
600k
    if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
294
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
295
0
        return 0;
296
0
    }
297
298
600k
    prov = OSSL_ENCODER_get0_provider(encoder);
299
600k
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
300
301
600k
    if ((encoderctx = encoder->newctx(provctx)) == NULL
302
600k
        || (encoder_inst = ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
303
0
        goto err;
304
    /* Avoid double free of encoderctx on further errors */
305
600k
    encoderctx = NULL;
306
307
600k
    if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
308
0
        goto err;
309
310
600k
    return 1;
311
0
err:
312
0
    ossl_encoder_instance_free(encoder_inst);
313
0
    if (encoderctx != NULL)
314
0
        encoder->freectx(encoderctx);
315
0
    return 0;
316
600k
}
317
318
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
319
    OSSL_LIB_CTX *libctx, const char *propq)
320
77.8k
{
321
77.8k
    return 1;
322
77.8k
}
323
324
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
325
357k
{
326
357k
    if (ctx == NULL || ctx->encoder_insts == NULL)
327
1.42k
        return 0;
328
356k
    return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
329
357k
}
330
331
int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
332
    OSSL_ENCODER_CONSTRUCT *construct)
333
76.5k
{
334
76.5k
    if (!ossl_assert(ctx != NULL)) {
335
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
336
0
        return 0;
337
0
    }
338
76.5k
    ctx->construct = construct;
339
76.5k
    return 1;
340
76.5k
}
341
342
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
343
    void *construct_data)
344
76.5k
{
345
76.5k
    if (!ossl_assert(ctx != NULL)) {
346
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
347
0
        return 0;
348
0
    }
349
76.5k
    ctx->construct_data = construct_data;
350
76.5k
    return 1;
351
76.5k
}
352
353
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
354
    OSSL_ENCODER_CLEANUP *cleanup)
355
76.5k
{
356
76.5k
    if (!ossl_assert(ctx != NULL)) {
357
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
358
0
        return 0;
359
0
    }
360
76.5k
    ctx->cleanup = cleanup;
361
76.5k
    return 1;
362
76.5k
}
363
364
OSSL_ENCODER *
365
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
366
1.56M
{
367
1.56M
    if (encoder_inst == NULL)
368
0
        return NULL;
369
1.56M
    return encoder_inst->encoder;
370
1.56M
}
371
372
void *
373
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
374
1.45M
{
375
1.45M
    if (encoder_inst == NULL)
376
0
        return NULL;
377
1.45M
    return encoder_inst->encoderctx;
378
1.45M
}
379
380
const char *
381
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
382
729k
{
383
729k
    if (encoder_inst == NULL)
384
0
        return NULL;
385
729k
    return encoder_inst->output_type;
386
729k
}
387
388
const char *
389
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
390
729k
{
391
729k
    if (encoder_inst == NULL)
392
0
        return NULL;
393
729k
    return encoder_inst->output_structure;
394
729k
}
395
396
static int encoder_process(struct encoder_process_data_st *data)
397
183k
{
398
183k
    OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
399
183k
    OSSL_ENCODER *current_encoder = NULL;
400
183k
    OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
401
183k
    BIO *allocated_out = NULL;
402
183k
    const void *original_data = NULL;
403
183k
    OSSL_PARAM abstract[10];
404
183k
    const OSSL_PARAM *current_abstract = NULL;
405
183k
    int i;
406
183k
    int ok = -1; /* -1 signifies that the lookup loop gave nothing */
407
183k
    int top = 0;
408
409
183k
    if (data->next_encoder_inst == NULL) {
410
        /* First iteration, where we prepare for what is to come */
411
412
91.8k
        data->count_output_structure = data->ctx->output_structure == NULL ? -1 : 0;
413
91.8k
        top = 1;
414
91.8k
    }
415
416
821k
    for (i = data->current_encoder_inst_index; i-- > 0;) {
417
729k
        OSSL_ENCODER *next_encoder = NULL;
418
729k
        const char *current_output_type;
419
729k
        const char *current_output_structure;
420
729k
        struct encoder_process_data_st new_data;
421
422
729k
        if (!top)
423
17.4k
            next_encoder = OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
424
425
729k
        current_encoder_inst = sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
426
729k
        current_encoder = OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
427
729k
        current_encoder_ctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
428
729k
        current_output_type = OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
429
729k
        current_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
430
729k
        memset(&new_data, 0, sizeof(new_data));
431
729k
        new_data.ctx = data->ctx;
432
729k
        new_data.current_encoder_inst_index = i;
433
729k
        new_data.next_encoder_inst = current_encoder_inst;
434
729k
        new_data.count_output_structure = data->count_output_structure;
435
729k
        new_data.level = data->level + 1;
436
437
729k
        OSSL_TRACE_BEGIN(ENCODER)
438
0
        {
439
0
            BIO_printf(trc_out,
440
0
                "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
441
0
                data->level, (void *)data->ctx,
442
0
                (void *)current_encoder_inst, (void *)current_encoder);
443
0
        }
444
729k
        OSSL_TRACE_END(ENCODER);
445
446
        /*
447
         * If this is the top call, we check if the output type of the current
448
         * encoder matches the desired output type.
449
         * If this isn't the top call, i.e. this is deeper in the recursion,
450
         * we instead check if the output type of the current encoder matches
451
         * the name of the next encoder (the one found by the parent call).
452
         */
453
729k
        if (top) {
454
712k
            if (data->ctx->output_type != NULL
455
712k
                && OPENSSL_strcasecmp(current_output_type,
456
712k
                       data->ctx->output_type)
457
712k
                    != 0) {
458
583k
                OSSL_TRACE_BEGIN(ENCODER)
459
0
                {
460
0
                    BIO_printf(trc_out,
461
0
                        "[%d]    Skipping because current encoder output type (%s) != desired output type (%s)\n",
462
0
                        data->level,
463
0
                        current_output_type, data->ctx->output_type);
464
0
                }
465
583k
                OSSL_TRACE_END(ENCODER);
466
583k
                continue;
467
583k
            }
468
712k
        } else {
469
17.4k
            if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
470
17.4k
                OSSL_TRACE_BEGIN(ENCODER)
471
0
                {
472
0
                    BIO_printf(trc_out,
473
0
                        "[%d]    Skipping because current encoder output type (%s) != name of encoder %p\n",
474
0
                        data->level,
475
0
                        current_output_type, (void *)next_encoder);
476
0
                }
477
17.4k
                OSSL_TRACE_END(ENCODER);
478
17.4k
                continue;
479
17.4k
            }
480
17.4k
        }
481
482
        /*
483
         * If the caller and the current encoder specify an output structure,
484
         * Check if they match.  If they do, count the match, otherwise skip
485
         * the current encoder.
486
         */
487
128k
        if (data->ctx->output_structure != NULL
488
48.4k
            && current_output_structure != NULL) {
489
48.4k
            if (OPENSSL_strcasecmp(data->ctx->output_structure,
490
48.4k
                    current_output_structure)
491
48.4k
                != 0) {
492
37.3k
                OSSL_TRACE_BEGIN(ENCODER)
493
0
                {
494
0
                    BIO_printf(trc_out,
495
0
                        "[%d]    Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
496
0
                        data->level,
497
0
                        current_output_structure,
498
0
                        data->ctx->output_structure);
499
0
                }
500
37.3k
                OSSL_TRACE_END(ENCODER);
501
37.3k
                continue;
502
37.3k
            }
503
504
11.0k
            data->count_output_structure++;
505
11.0k
        }
506
507
        /*
508
         * Recurse to process the encoder implementations before the current
509
         * one.
510
         */
511
91.5k
        ok = encoder_process(&new_data);
512
513
91.5k
        data->prev_encoder_inst = new_data.prev_encoder_inst;
514
91.5k
        data->running_output = new_data.running_output;
515
91.5k
        data->running_output_length = new_data.running_output_length;
516
517
        /*
518
         * ok == -1     means that the recursion call above gave no further
519
         *              encoders, and that the one we're currently at should
520
         *              be tried.
521
         * ok == 0      means that something failed in the recursion call
522
         *              above, making the result unsuitable for a chain.
523
         *              In this case, we simply continue to try finding a
524
         *              suitable encoder at this recursion level.
525
         * ok == 1      means that the recursion call was successful, and we
526
         *              try to use the result at this recursion level.
527
         */
528
91.5k
        if (ok != 0)
529
91.5k
            break;
530
531
0
        OSSL_TRACE_BEGIN(ENCODER)
532
0
        {
533
0
            BIO_printf(trc_out,
534
0
                "[%d]    Skipping because recusion level %d failed\n",
535
0
                data->level, new_data.level);
536
0
        }
537
0
        OSSL_TRACE_END(ENCODER);
538
0
    }
539
540
    /*
541
     * If |i < 0|, we didn't find any useful encoder in this recursion, so
542
     * we do the rest of the process only if |i >= 0|.
543
     */
544
183k
    if (i < 0) {
545
91.8k
        ok = -1;
546
547
91.8k
        OSSL_TRACE_BEGIN(ENCODER)
548
0
        {
549
0
            BIO_printf(trc_out,
550
0
                "[%d] (ctx %p) No suitable encoder found\n",
551
0
                data->level, (void *)data->ctx);
552
0
        }
553
91.8k
        OSSL_TRACE_END(ENCODER);
554
91.8k
    } else {
555
        /* Preparations */
556
557
91.5k
        switch (ok) {
558
0
        case 0:
559
0
            break;
560
91.5k
        case -1:
561
            /*
562
             * We have reached the beginning of the encoder instance sequence,
563
             * so we prepare the object to be encoded.
564
             */
565
566
            /*
567
             * |data->count_output_structure| is one of these values:
568
             *
569
             * -1       There is no desired output structure
570
             *  0       There is a desired output structure, and it wasn't
571
             *          matched by any of the encoder instances that were
572
             *          considered
573
             * >0       There is a desired output structure, and at least one
574
             *          of the encoder instances matched it
575
             */
576
91.5k
            if (data->count_output_structure == 0)
577
0
                return 0;
578
579
91.5k
            original_data = data->ctx->construct(current_encoder_inst,
580
91.5k
                data->ctx->construct_data);
581
582
            /* Also set the data type, using the encoder implementation name */
583
91.5k
            data->data_type = OSSL_ENCODER_get0_name(current_encoder);
584
585
            /* Assume that the constructor recorded an error */
586
91.5k
            if (original_data != NULL)
587
91.5k
                ok = 1;
588
0
            else
589
0
                ok = 0;
590
91.5k
            break;
591
0
        case 1:
592
0
            if (!ossl_assert(data->running_output != NULL)) {
593
0
                ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
594
0
                ok = 0;
595
0
                break;
596
0
            }
597
598
0
            {
599
                /*
600
                 * Create an object abstraction from the latest output, which
601
                 * was stolen from the previous round.
602
                 */
603
604
0
                OSSL_PARAM *abstract_p = abstract;
605
0
                const char *prev_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
606
607
0
                *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
608
0
                    (char *)data->data_type, 0);
609
0
                if (prev_output_structure != NULL)
610
0
                    *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
611
0
                        (char *)prev_output_structure,
612
0
                        0);
613
0
                *abstract_p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
614
0
                    data->running_output,
615
0
                    data->running_output_length);
616
0
                *abstract_p = OSSL_PARAM_construct_end();
617
0
                current_abstract = abstract;
618
0
            }
619
0
            break;
620
91.5k
        }
621
622
        /* Calling the encoder implementation */
623
624
91.5k
        if (ok) {
625
91.5k
            OSSL_CORE_BIO *cbio = NULL;
626
91.5k
            BIO *current_out = NULL;
627
628
            /*
629
             * If we're at the last encoder instance to use, we're setting up
630
             * final output.  Otherwise, set up an intermediary memory output.
631
             */
632
91.5k
            if (top)
633
91.5k
                current_out = data->bio;
634
0
            else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
635
0
                == NULL)
636
0
                ok = 0; /* Assume BIO_new() recorded an error */
637
638
91.5k
            if (ok)
639
91.5k
                ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
640
91.5k
            if (ok) {
641
91.5k
                ok = current_encoder->encode(current_encoder_ctx, cbio,
642
91.5k
                    original_data, current_abstract,
643
91.5k
                    data->ctx->selection,
644
91.5k
                    ossl_pw_passphrase_callback_enc,
645
91.5k
                    &data->ctx->pwdata);
646
91.5k
                OSSL_TRACE_BEGIN(ENCODER)
647
0
                {
648
0
                    BIO_printf(trc_out,
649
0
                        "[%d] (ctx %p) Running encoder instance %p => %d\n",
650
0
                        data->level, (void *)data->ctx,
651
0
                        (void *)current_encoder_inst, ok);
652
0
                }
653
91.5k
                OSSL_TRACE_END(ENCODER);
654
91.5k
            }
655
656
91.5k
            ossl_core_bio_free(cbio);
657
91.5k
            data->prev_encoder_inst = current_encoder_inst;
658
91.5k
        }
659
91.5k
    }
660
661
    /* Cleanup and collecting the result */
662
663
183k
    OPENSSL_free(data->running_output);
664
183k
    data->running_output = NULL;
665
666
    /*
667
     * Steal the output from the BIO_s_mem, if we did allocate one.
668
     * That'll be the data for an object abstraction in the next round.
669
     */
670
183k
    if (allocated_out != NULL) {
671
0
        BUF_MEM *buf;
672
673
0
        BIO_get_mem_ptr(allocated_out, &buf);
674
0
        data->running_output = (unsigned char *)buf->data;
675
0
        data->running_output_length = buf->length;
676
0
        memset(buf, 0, sizeof(*buf));
677
0
    }
678
679
183k
    BIO_free(allocated_out);
680
183k
    if (original_data != NULL)
681
91.5k
        data->ctx->cleanup(data->ctx->construct_data);
682
183k
    return ok;
683
183k
}