Coverage Report

Created: 2023-09-25 06:45

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