Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
        return 0;
203
204
600k
    if (!OSSL_ENCODER_up_ref(encoder)) {
205
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
206
0
        goto err;
207
0
    }
208
209
600k
    prov = OSSL_ENCODER_get0_provider(encoder);
210
600k
    libctx = ossl_provider_libctx(prov);
211
600k
    props = ossl_encoder_parsed_properties(encoder);
212
600k
    if (props == NULL) {
213
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
214
0
            "there are no property definitions with encoder %s",
215
0
            OSSL_ENCODER_get0_name(encoder));
216
0
        goto err;
217
0
    }
218
219
    /* The "output" property is mandatory */
220
600k
    prop = ossl_property_find_property(props, libctx, "output");
221
600k
    encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
222
600k
    if (encoder_inst->output_type == NULL) {
223
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
224
0
            "the mandatory 'output' property is missing "
225
0
            "for encoder %s (properties: %s)",
226
0
            OSSL_ENCODER_get0_name(encoder),
227
0
            OSSL_ENCODER_get0_properties(encoder));
228
0
        goto err;
229
0
    }
230
231
    /* The "structure" property is optional */
232
600k
    prop = ossl_property_find_property(props, libctx, "structure");
233
600k
    if (prop != NULL)
234
467k
        encoder_inst->output_structure
235
467k
            = ossl_property_get_string_value(libctx, prop);
236
237
600k
    encoder_inst->encoder = encoder;
238
600k
    encoder_inst->encoderctx = encoderctx;
239
600k
    return encoder_inst;
240
0
err:
241
0
    ossl_encoder_instance_free(encoder_inst);
242
0
    return NULL;
243
600k
}
244
245
void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
246
729k
{
247
729k
    if (encoder_inst != NULL) {
248
729k
        if (encoder_inst->encoder != NULL)
249
729k
            encoder_inst->encoder->freectx(encoder_inst->encoderctx);
250
729k
        encoder_inst->encoderctx = NULL;
251
729k
        OSSL_ENCODER_free(encoder_inst->encoder);
252
729k
        encoder_inst->encoder = NULL;
253
729k
        OPENSSL_free(encoder_inst);
254
729k
    }
255
729k
}
256
257
static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
258
    OSSL_ENCODER_INSTANCE *ei)
259
729k
{
260
729k
    int ok;
261
262
729k
    if (ctx->encoder_insts == NULL
263
91.8k
        && (ctx->encoder_insts = sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
264
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
265
0
        return 0;
266
0
    }
267
268
729k
    ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
269
729k
    if (ok) {
270
729k
        OSSL_TRACE_BEGIN(ENCODER)
271
0
        {
272
0
            BIO_printf(trc_out,
273
0
                "(ctx %p) Added encoder instance %p (encoder %p):\n"
274
0
                "    %s with %s\n",
275
0
                (void *)ctx, (void *)ei, (void *)ei->encoder,
276
0
                OSSL_ENCODER_get0_name(ei->encoder),
277
0
                OSSL_ENCODER_get0_properties(ei->encoder));
278
0
        }
279
729k
        OSSL_TRACE_END(ENCODER);
280
729k
    }
281
729k
    return ok;
282
729k
}
283
284
int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
285
600k
{
286
600k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
287
600k
    const OSSL_PROVIDER *prov = NULL;
288
600k
    void *encoderctx = NULL;
289
600k
    void *provctx = NULL;
290
291
600k
    if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
292
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
293
0
        return 0;
294
0
    }
295
296
600k
    prov = OSSL_ENCODER_get0_provider(encoder);
297
600k
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
298
299
600k
    if ((encoderctx = encoder->newctx(provctx)) == NULL
300
600k
        || (encoder_inst = ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
301
0
        goto err;
302
    /* Avoid double free of encoderctx on further errors */
303
600k
    encoderctx = NULL;
304
305
600k
    if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
306
0
        goto err;
307
308
600k
    return 1;
309
0
err:
310
0
    ossl_encoder_instance_free(encoder_inst);
311
0
    if (encoderctx != NULL)
312
0
        encoder->freectx(encoderctx);
313
0
    return 0;
314
600k
}
315
316
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
317
    OSSL_LIB_CTX *libctx, const char *propq)
318
77.8k
{
319
77.8k
    return 1;
320
77.8k
}
321
322
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
323
357k
{
324
357k
    if (ctx == NULL || ctx->encoder_insts == NULL)
325
1.42k
        return 0;
326
356k
    return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
327
357k
}
328
329
int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
330
    OSSL_ENCODER_CONSTRUCT *construct)
331
76.5k
{
332
76.5k
    if (!ossl_assert(ctx != NULL)) {
333
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
334
0
        return 0;
335
0
    }
336
76.5k
    ctx->construct = construct;
337
76.5k
    return 1;
338
76.5k
}
339
340
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
341
    void *construct_data)
342
76.5k
{
343
76.5k
    if (!ossl_assert(ctx != NULL)) {
344
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
345
0
        return 0;
346
0
    }
347
76.5k
    ctx->construct_data = construct_data;
348
76.5k
    return 1;
349
76.5k
}
350
351
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
352
    OSSL_ENCODER_CLEANUP *cleanup)
353
76.5k
{
354
76.5k
    if (!ossl_assert(ctx != NULL)) {
355
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
356
0
        return 0;
357
0
    }
358
76.5k
    ctx->cleanup = cleanup;
359
76.5k
    return 1;
360
76.5k
}
361
362
OSSL_ENCODER *
363
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
364
1.56M
{
365
1.56M
    if (encoder_inst == NULL)
366
0
        return NULL;
367
1.56M
    return encoder_inst->encoder;
368
1.56M
}
369
370
void *
371
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
372
1.45M
{
373
1.45M
    if (encoder_inst == NULL)
374
0
        return NULL;
375
1.45M
    return encoder_inst->encoderctx;
376
1.45M
}
377
378
const char *
379
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
380
729k
{
381
729k
    if (encoder_inst == NULL)
382
0
        return NULL;
383
729k
    return encoder_inst->output_type;
384
729k
}
385
386
const char *
387
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
388
729k
{
389
729k
    if (encoder_inst == NULL)
390
0
        return NULL;
391
729k
    return encoder_inst->output_structure;
392
729k
}
393
394
static int encoder_process(struct encoder_process_data_st *data)
395
183k
{
396
183k
    OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
397
183k
    OSSL_ENCODER *current_encoder = NULL;
398
183k
    OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
399
183k
    BIO *allocated_out = NULL;
400
183k
    const void *original_data = NULL;
401
183k
    OSSL_PARAM abstract[10];
402
183k
    const OSSL_PARAM *current_abstract = NULL;
403
183k
    int i;
404
183k
    int ok = -1; /* -1 signifies that the lookup loop gave nothing */
405
183k
    int top = 0;
406
407
183k
    if (data->next_encoder_inst == NULL) {
408
        /* First iteration, where we prepare for what is to come */
409
410
91.8k
        data->count_output_structure = data->ctx->output_structure == NULL ? -1 : 0;
411
91.8k
        top = 1;
412
91.8k
    }
413
414
821k
    for (i = data->current_encoder_inst_index; i-- > 0;) {
415
729k
        OSSL_ENCODER *next_encoder = NULL;
416
729k
        const char *current_output_type;
417
729k
        const char *current_output_structure;
418
729k
        struct encoder_process_data_st new_data;
419
420
729k
        if (!top)
421
17.4k
            next_encoder = OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
422
423
729k
        current_encoder_inst = sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
424
729k
        current_encoder = OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
425
729k
        current_encoder_ctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
426
729k
        current_output_type = OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
427
729k
        current_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
428
729k
        memset(&new_data, 0, sizeof(new_data));
429
729k
        new_data.ctx = data->ctx;
430
729k
        new_data.current_encoder_inst_index = i;
431
729k
        new_data.next_encoder_inst = current_encoder_inst;
432
729k
        new_data.count_output_structure = data->count_output_structure;
433
729k
        new_data.level = data->level + 1;
434
435
729k
        OSSL_TRACE_BEGIN(ENCODER)
436
0
        {
437
0
            BIO_printf(trc_out,
438
0
                "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
439
0
                data->level, (void *)data->ctx,
440
0
                (void *)current_encoder_inst, (void *)current_encoder);
441
0
        }
442
729k
        OSSL_TRACE_END(ENCODER);
443
444
        /*
445
         * If this is the top call, we check if the output type of the current
446
         * encoder matches the desired output type.
447
         * If this isn't the top call, i.e. this is deeper in the recursion,
448
         * we instead check if the output type of the current encoder matches
449
         * the name of the next encoder (the one found by the parent call).
450
         */
451
729k
        if (top) {
452
712k
            if (data->ctx->output_type != NULL
453
712k
                && OPENSSL_strcasecmp(current_output_type,
454
712k
                       data->ctx->output_type)
455
712k
                    != 0) {
456
583k
                OSSL_TRACE_BEGIN(ENCODER)
457
0
                {
458
0
                    BIO_printf(trc_out,
459
0
                        "[%d]    Skipping because current encoder output type (%s) != desired output type (%s)\n",
460
0
                        data->level,
461
0
                        current_output_type, data->ctx->output_type);
462
0
                }
463
583k
                OSSL_TRACE_END(ENCODER);
464
583k
                continue;
465
583k
            }
466
712k
        } else {
467
17.4k
            if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
468
17.4k
                OSSL_TRACE_BEGIN(ENCODER)
469
0
                {
470
0
                    BIO_printf(trc_out,
471
0
                        "[%d]    Skipping because current encoder output type (%s) != name of encoder %p\n",
472
0
                        data->level,
473
0
                        current_output_type, (void *)next_encoder);
474
0
                }
475
17.4k
                OSSL_TRACE_END(ENCODER);
476
17.4k
                continue;
477
17.4k
            }
478
17.4k
        }
479
480
        /*
481
         * If the caller and the current encoder specify an output structure,
482
         * Check if they match.  If they do, count the match, otherwise skip
483
         * the current encoder.
484
         */
485
128k
        if (data->ctx->output_structure != NULL
486
48.4k
            && current_output_structure != NULL) {
487
48.4k
            if (OPENSSL_strcasecmp(data->ctx->output_structure,
488
48.4k
                    current_output_structure)
489
48.4k
                != 0) {
490
37.3k
                OSSL_TRACE_BEGIN(ENCODER)
491
0
                {
492
0
                    BIO_printf(trc_out,
493
0
                        "[%d]    Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
494
0
                        data->level,
495
0
                        current_output_structure,
496
0
                        data->ctx->output_structure);
497
0
                }
498
37.3k
                OSSL_TRACE_END(ENCODER);
499
37.3k
                continue;
500
37.3k
            }
501
502
11.0k
            data->count_output_structure++;
503
11.0k
        }
504
505
        /*
506
         * Recurse to process the encoder implementations before the current
507
         * one.
508
         */
509
91.5k
        ok = encoder_process(&new_data);
510
511
91.5k
        data->prev_encoder_inst = new_data.prev_encoder_inst;
512
91.5k
        data->running_output = new_data.running_output;
513
91.5k
        data->running_output_length = new_data.running_output_length;
514
515
        /*
516
         * ok == -1     means that the recursion call above gave no further
517
         *              encoders, and that the one we're currently at should
518
         *              be tried.
519
         * ok == 0      means that something failed in the recursion call
520
         *              above, making the result unsuitable for a chain.
521
         *              In this case, we simply continue to try finding a
522
         *              suitable encoder at this recursion level.
523
         * ok == 1      means that the recursion call was successful, and we
524
         *              try to use the result at this recursion level.
525
         */
526
91.5k
        if (ok != 0)
527
91.5k
            break;
528
529
0
        OSSL_TRACE_BEGIN(ENCODER)
530
0
        {
531
0
            BIO_printf(trc_out,
532
0
                "[%d]    Skipping because recursion level %d failed\n",
533
0
                data->level, new_data.level);
534
0
        }
535
0
        OSSL_TRACE_END(ENCODER);
536
0
    }
537
538
    /*
539
     * If |i < 0|, we didn't find any useful encoder in this recursion, so
540
     * we do the rest of the process only if |i >= 0|.
541
     */
542
183k
    if (i < 0) {
543
91.8k
        ok = -1;
544
545
91.8k
        OSSL_TRACE_BEGIN(ENCODER)
546
0
        {
547
0
            BIO_printf(trc_out,
548
0
                "[%d] (ctx %p) No suitable encoder found\n",
549
0
                data->level, (void *)data->ctx);
550
0
        }
551
91.8k
        OSSL_TRACE_END(ENCODER);
552
91.8k
    } else {
553
        /* Preparations */
554
555
91.5k
        switch (ok) {
556
0
        case 0:
557
0
            break;
558
91.5k
        case -1:
559
            /*
560
             * We have reached the beginning of the encoder instance sequence,
561
             * so we prepare the object to be encoded.
562
             */
563
564
            /*
565
             * |data->count_output_structure| is one of these values:
566
             *
567
             * -1       There is no desired output structure
568
             *  0       There is a desired output structure, and it wasn't
569
             *          matched by any of the encoder instances that were
570
             *          considered
571
             * >0       There is a desired output structure, and at least one
572
             *          of the encoder instances matched it
573
             */
574
91.5k
            if (data->count_output_structure == 0)
575
0
                return 0;
576
577
91.5k
            original_data = data->ctx->construct(current_encoder_inst,
578
91.5k
                data->ctx->construct_data);
579
580
            /* Also set the data type, using the encoder implementation name */
581
91.5k
            data->data_type = OSSL_ENCODER_get0_name(current_encoder);
582
583
            /* Assume that the constructor recorded an error */
584
91.5k
            if (original_data != NULL)
585
91.5k
                ok = 1;
586
0
            else
587
0
                ok = 0;
588
91.5k
            break;
589
0
        case 1:
590
0
            if (!ossl_assert(data->running_output != NULL)) {
591
0
                ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
592
0
                ok = 0;
593
0
                break;
594
0
            }
595
596
0
            {
597
                /*
598
                 * Create an object abstraction from the latest output, which
599
                 * was stolen from the previous round.
600
                 */
601
602
0
                OSSL_PARAM *abstract_p = abstract;
603
0
                const char *prev_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
604
605
0
                *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
606
0
                    (char *)data->data_type, 0);
607
0
                if (prev_output_structure != NULL)
608
0
                    *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
609
0
                        (char *)prev_output_structure,
610
0
                        0);
611
0
                *abstract_p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
612
0
                    data->running_output,
613
0
                    data->running_output_length);
614
0
                *abstract_p = OSSL_PARAM_construct_end();
615
0
                current_abstract = abstract;
616
0
            }
617
0
            break;
618
91.5k
        }
619
620
        /* Calling the encoder implementation */
621
622
91.5k
        if (ok) {
623
91.5k
            OSSL_CORE_BIO *cbio = NULL;
624
91.5k
            BIO *current_out = NULL;
625
626
            /*
627
             * If we're at the last encoder instance to use, we're setting up
628
             * final output.  Otherwise, set up an intermediary memory output.
629
             */
630
91.5k
            if (top)
631
91.5k
                current_out = data->bio;
632
0
            else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
633
0
                == NULL)
634
0
                ok = 0; /* Assume BIO_new() recorded an error */
635
636
91.5k
            if (ok)
637
91.5k
                ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
638
91.5k
            if (ok) {
639
91.5k
                ok = current_encoder->encode(current_encoder_ctx, cbio,
640
91.5k
                    original_data, current_abstract,
641
91.5k
                    data->ctx->selection,
642
91.5k
                    ossl_pw_passphrase_callback_enc,
643
91.5k
                    &data->ctx->pwdata);
644
91.5k
                OSSL_TRACE_BEGIN(ENCODER)
645
0
                {
646
0
                    BIO_printf(trc_out,
647
0
                        "[%d] (ctx %p) Running encoder instance %p => %d\n",
648
0
                        data->level, (void *)data->ctx,
649
0
                        (void *)current_encoder_inst, ok);
650
0
                }
651
91.5k
                OSSL_TRACE_END(ENCODER);
652
91.5k
            }
653
654
91.5k
            ossl_core_bio_free(cbio);
655
91.5k
            data->prev_encoder_inst = current_encoder_inst;
656
91.5k
        }
657
91.5k
    }
658
659
    /* Cleanup and collecting the result */
660
661
183k
    OPENSSL_free(data->running_output);
662
183k
    data->running_output = NULL;
663
664
    /*
665
     * Steal the output from the BIO_s_mem, if we did allocate one.
666
     * That'll be the data for an object abstraction in the next round.
667
     */
668
183k
    if (allocated_out != NULL) {
669
0
        BUF_MEM *buf;
670
671
0
        BIO_get_mem_ptr(allocated_out, &buf);
672
0
        data->running_output = (unsigned char *)buf->data;
673
0
        data->running_output_length = buf->length;
674
0
        memset(buf, 0, sizeof(*buf));
675
0
    }
676
677
183k
    BIO_free(allocated_out);
678
183k
    if (original_data != NULL)
679
91.5k
        data->ctx->cleanup(data->ctx->construct_data);
680
183k
    return ok;
681
183k
}