Coverage Report

Created: 2023-09-25 06:41

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