Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/crypto/encode_decode/encoder_lib.c
Line
Count
Source (jump to first uncovered line)
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
42.1k
{
47
42.1k
    struct encoder_process_data_st data;
48
49
42.1k
    memset(&data, 0, sizeof(data));
50
42.1k
    data.ctx = ctx;
51
42.1k
    data.bio = out;
52
42.1k
    data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
53
54
42.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
42.1k
    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
42.1k
    return encoder_process(&data) > 0;
68
42.1k
}
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
6.07k
{
99
6.07k
    BIO *out;
100
6.07k
    BUF_MEM *buf = NULL;
101
6.07k
    int ret = 0;
102
103
6.07k
    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
6.07k
    out = BIO_new(BIO_s_mem());
109
110
6.07k
    if (out != NULL
111
6.07k
        && OSSL_ENCODER_to_bio(ctx, out)
112
6.07k
        && BIO_get_mem_ptr(out, &buf) > 0) {
113
5.03k
        ret = 1; /* Hope for the best. A too small buffer will clear this */
114
115
5.03k
        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
5.03k
        } else {
126
            /* The buffer with the right size is already allocated for us */
127
5.03k
            *pdata_len = (size_t)buf->length;
128
5.03k
        }
129
130
5.03k
        if (ret) {
131
5.03k
            if (pdata != NULL) {
132
5.03k
                if (*pdata != NULL) {
133
0
                    memcpy(*pdata, buf->data, buf->length);
134
0
                    *pdata += buf->length;
135
5.03k
                } else {
136
                    /* In this case, we steal the data from BIO_s_mem() */
137
5.03k
                    *pdata = (unsigned char *)buf->data;
138
5.03k
                    buf->data = NULL;
139
5.03k
                }
140
5.03k
            }
141
5.03k
        }
142
5.03k
    }
143
6.07k
    BIO_free(out);
144
6.07k
    return ret;
145
6.07k
}
146
147
int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
148
42.7k
{
149
42.7k
    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
42.7k
    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
42.7k
    ctx->selection = selection;
160
42.7k
    return 1;
161
42.7k
}
162
163
int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
164
                                     const char *output_type)
165
42.7k
{
166
42.7k
    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
42.7k
    ctx->output_type = output_type;
172
42.7k
    return 1;
173
42.7k
}
174
175
int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
176
                                          const char *output_structure)
177
6.07k
{
178
6.07k
    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
6.07k
    ctx->output_structure = output_structure;
184
6.07k
    return 1;
185
6.07k
}
186
187
static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
188
                                                        void *encoderctx)
189
335k
{
190
335k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
191
335k
    const OSSL_PROVIDER *prov;
192
335k
    OSSL_LIB_CTX *libctx;
193
335k
    const OSSL_PROPERTY_LIST *props;
194
335k
    const OSSL_PROPERTY_DEFINITION *prop;
195
196
335k
    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
335k
    if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL)
202
0
        return 0;
203
204
335k
    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
335k
    prov = OSSL_ENCODER_get0_provider(encoder);
210
335k
    libctx = ossl_provider_libctx(prov);
211
335k
    props = ossl_encoder_parsed_properties(encoder);
212
335k
    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
335k
    prop = ossl_property_find_property(props, libctx, "output");
221
335k
    encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
222
335k
    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
335k
    prop = ossl_property_find_property(props, libctx, "structure");
233
335k
    if (prop != NULL)
234
260k
        encoder_inst->output_structure
235
260k
            = ossl_property_get_string_value(libctx, prop);
236
237
335k
    encoder_inst->encoder = encoder;
238
335k
    encoder_inst->encoderctx = encoderctx;
239
335k
    return encoder_inst;
240
0
 err:
241
0
    ossl_encoder_instance_free(encoder_inst);
242
0
    return NULL;
243
335k
}
244
245
void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
246
335k
{
247
335k
    if (encoder_inst != NULL) {
248
335k
        if (encoder_inst->encoder != NULL)
249
335k
            encoder_inst->encoder->freectx(encoder_inst->encoderctx);
250
335k
        encoder_inst->encoderctx = NULL;
251
335k
        OSSL_ENCODER_free(encoder_inst->encoder);
252
335k
        encoder_inst->encoder = NULL;
253
335k
        OPENSSL_free(encoder_inst);
254
335k
    }
255
335k
}
256
257
static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
258
                                             OSSL_ENCODER_INSTANCE *ei)
259
335k
{
260
335k
    int ok;
261
262
335k
    if (ctx->encoder_insts == NULL
263
335k
        && (ctx->encoder_insts =
264
42.1k
            sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
265
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
266
0
        return 0;
267
0
    }
268
269
335k
    ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
270
335k
    if (ok) {
271
335k
        OSSL_TRACE_BEGIN(ENCODER) {
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
335k
        } OSSL_TRACE_END(ENCODER);
279
335k
    }
280
335k
    return ok;
281
335k
}
282
283
int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
284
335k
{
285
335k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
286
335k
    const OSSL_PROVIDER *prov = NULL;
287
335k
    void *encoderctx = NULL;
288
335k
    void *provctx = NULL;
289
290
335k
    if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
291
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
292
0
        return 0;
293
0
    }
294
295
335k
    prov = OSSL_ENCODER_get0_provider(encoder);
296
335k
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
297
298
335k
    if ((encoderctx = encoder->newctx(provctx)) == NULL
299
335k
        || (encoder_inst =
300
335k
            ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
301
0
        goto err;
302
    /* Avoid double free of encoderctx on further errors */
303
335k
    encoderctx = NULL;
304
305
335k
    if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
306
0
        goto err;
307
308
335k
    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
335k
}
315
316
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
317
                               OSSL_LIB_CTX *libctx, const char *propq)
318
42.7k
{
319
42.7k
    return 1;
320
42.7k
}
321
322
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
323
163k
{
324
163k
    if (ctx == NULL || ctx->encoder_insts == NULL)
325
566
        return 0;
326
162k
    return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
327
163k
}
328
329
int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
330
                                   OSSL_ENCODER_CONSTRUCT *construct)
331
42.1k
{
332
42.1k
    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
42.1k
    ctx->construct = construct;
337
42.1k
    return 1;
338
42.1k
}
339
340
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
341
                                        void *construct_data)
342
42.1k
{
343
42.1k
    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
42.1k
    ctx->construct_data = construct_data;
348
42.1k
    return 1;
349
42.1k
}
350
351
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
352
                                 OSSL_ENCODER_CLEANUP *cleanup)
353
42.1k
{
354
42.1k
    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
42.1k
    ctx->cleanup = cleanup;
359
42.1k
    return 1;
360
42.1k
}
361
362
OSSL_ENCODER *
363
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
364
720k
{
365
720k
    if (encoder_inst == NULL)
366
0
        return NULL;
367
720k
    return encoder_inst->encoder;
368
720k
}
369
370
void *
371
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
372
670k
{
373
670k
    if (encoder_inst == NULL)
374
0
        return NULL;
375
670k
    return encoder_inst->encoderctx;
376
670k
}
377
378
const char *
379
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
380
335k
{
381
335k
    if (encoder_inst == NULL)
382
0
        return NULL;
383
335k
    return encoder_inst->output_type;
384
335k
}
385
386
const char *
387
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
388
335k
{
389
335k
    if (encoder_inst == NULL)
390
0
        return NULL;
391
335k
    return encoder_inst->output_structure;
392
335k
}
393
394
static int encoder_process(struct encoder_process_data_st *data)
395
84.1k
{
396
84.1k
    OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
397
84.1k
    OSSL_ENCODER *current_encoder = NULL;
398
84.1k
    OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
399
84.1k
    BIO *allocated_out = NULL;
400
84.1k
    const void *original_data = NULL;
401
84.1k
    OSSL_PARAM abstract[10];
402
84.1k
    const OSSL_PARAM *current_abstract = NULL;
403
84.1k
    int i;
404
84.1k
    int ok = -1;  /* -1 signifies that the lookup loop gave nothing */
405
84.1k
    int top = 0;
406
407
84.1k
    if (data->next_encoder_inst == NULL) {
408
        /* First iteration, where we prepare for what is to come */
409
410
42.1k
        data->count_output_structure =
411
42.1k
            data->ctx->output_structure == NULL ? -1 : 0;
412
42.1k
        top = 1;
413
42.1k
    }
414
415
377k
    for (i = data->current_encoder_inst_index; i-- > 0;) {
416
335k
        OSSL_ENCODER *next_encoder = NULL;
417
335k
        const char *current_output_type;
418
335k
        const char *current_output_structure;
419
335k
        struct encoder_process_data_st new_data;
420
421
335k
        if (!top)
422
8.05k
            next_encoder =
423
8.05k
                OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
424
425
335k
        current_encoder_inst =
426
335k
            sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
427
335k
        current_encoder =
428
335k
            OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
429
335k
        current_encoder_ctx =
430
335k
            OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
431
335k
        current_output_type =
432
335k
            OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
433
335k
        current_output_structure =
434
335k
            OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
435
335k
        memset(&new_data, 0, sizeof(new_data));
436
335k
        new_data.ctx = data->ctx;
437
335k
        new_data.current_encoder_inst_index = i;
438
335k
        new_data.next_encoder_inst = current_encoder_inst;
439
335k
        new_data.count_output_structure = data->count_output_structure;
440
335k
        new_data.level = data->level + 1;
441
442
335k
        OSSL_TRACE_BEGIN(ENCODER) {
443
0
            BIO_printf(trc_out,
444
0
                       "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
445
0
                       data->level, (void *)data->ctx,
446
0
                       (void *)current_encoder_inst, (void *)current_encoder);
447
335k
        } OSSL_TRACE_END(ENCODER);
448
449
        /*
450
         * If this is the top call, we check if the output type of the current
451
         * encoder matches the desired output type.
452
         * If this isn't the top call, i.e. this is deeper in the recursion,
453
         * we instead check if the output type of the current encoder matches
454
         * the name of the next encoder (the one found by the parent call).
455
         */
456
335k
        if (top) {
457
327k
            if (data->ctx->output_type != NULL
458
327k
                && OPENSSL_strcasecmp(current_output_type,
459
327k
                                      data->ctx->output_type) != 0) {
460
264k
                OSSL_TRACE_BEGIN(ENCODER) {
461
0
                    BIO_printf(trc_out,
462
0
                               "[%d]    Skipping because current encoder output type (%s) != desired output type (%s)\n",
463
0
                               data->level,
464
0
                               current_output_type, data->ctx->output_type);
465
264k
                } OSSL_TRACE_END(ENCODER);
466
264k
                continue;
467
264k
            }
468
327k
        } else {
469
8.05k
            if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
470
8.05k
                OSSL_TRACE_BEGIN(ENCODER) {
471
0
                    BIO_printf(trc_out,
472
0
                               "[%d]    Skipping because current encoder output type (%s) != name of encoder %p\n",
473
0
                               data->level,
474
0
                               current_output_type, (void *)next_encoder);
475
8.05k
                } OSSL_TRACE_END(ENCODER);
476
8.05k
                continue;
477
8.05k
            }
478
8.05k
        }
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
63.0k
        if (data->ctx->output_structure != NULL
486
63.0k
            && current_output_structure != NULL) {
487
26.9k
            if (OPENSSL_strcasecmp(data->ctx->output_structure,
488
26.9k
                                   current_output_structure) != 0) {
489
20.9k
                OSSL_TRACE_BEGIN(ENCODER) {
490
0
                    BIO_printf(trc_out,
491
0
                               "[%d]    Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
492
0
                               data->level,
493
0
                               current_output_structure,
494
0
                               data->ctx->output_structure);
495
20.9k
                } OSSL_TRACE_END(ENCODER);
496
20.9k
                continue;
497
20.9k
            }
498
499
5.97k
            data->count_output_structure++;
500
5.97k
        }
501
502
        /*
503
         * Recurse to process the encoder implementations before the current
504
         * one.
505
         */
506
42.0k
        ok = encoder_process(&new_data);
507
508
42.0k
        data->prev_encoder_inst = new_data.prev_encoder_inst;
509
42.0k
        data->running_output = new_data.running_output;
510
42.0k
        data->running_output_length = new_data.running_output_length;
511
512
        /*
513
         * ok == -1     means that the recursion call above gave no further
514
         *              encoders, and that the one we're currently at should
515
         *              be tried.
516
         * ok == 0      means that something failed in the recursion call
517
         *              above, making the result unsuitable for a chain.
518
         *              In this case, we simply continue to try finding a
519
         *              suitable encoder at this recursion level.
520
         * ok == 1      means that the recursion call was successful, and we
521
         *              try to use the result at this recursion level.
522
         */
523
42.0k
        if (ok != 0)
524
42.0k
            break;
525
526
0
        OSSL_TRACE_BEGIN(ENCODER) {
527
0
            BIO_printf(trc_out,
528
0
                       "[%d]    Skipping because recursion level %d failed\n",
529
0
                       data->level, new_data.level);
530
0
        } OSSL_TRACE_END(ENCODER);
531
0
    }
532
533
    /*
534
     * If |i < 0|, we didn't find any useful encoder in this recursion, so
535
     * we do the rest of the process only if |i >= 0|.
536
     */
537
84.1k
    if (i < 0) {
538
42.1k
        ok = -1;
539
540
42.1k
        OSSL_TRACE_BEGIN(ENCODER) {
541
0
            BIO_printf(trc_out,
542
0
                       "[%d] (ctx %p) No suitable encoder found\n",
543
0
                       data->level, (void *)data->ctx);
544
42.1k
        } OSSL_TRACE_END(ENCODER);
545
42.1k
    } else {
546
        /* Preparations */
547
548
42.0k
        switch (ok) {
549
0
        case 0:
550
0
            break;
551
42.0k
        case -1:
552
            /*
553
             * We have reached the beginning of the encoder instance sequence,
554
             * so we prepare the object to be encoded.
555
             */
556
557
            /*
558
             * |data->count_output_structure| is one of these values:
559
             *
560
             * -1       There is no desired output structure
561
             *  0       There is a desired output structure, and it wasn't
562
             *          matched by any of the encoder instances that were
563
             *          considered
564
             * >0       There is a desired output structure, and at least one
565
             *          of the encoder instances matched it
566
             */
567
42.0k
            if (data->count_output_structure == 0)
568
0
                return 0;
569
570
42.0k
            original_data =
571
42.0k
                data->ctx->construct(current_encoder_inst,
572
42.0k
                                     data->ctx->construct_data);
573
574
            /* Also set the data type, using the encoder implementation name */
575
42.0k
            data->data_type = OSSL_ENCODER_get0_name(current_encoder);
576
577
            /* Assume that the constructor recorded an error */
578
42.0k
            if (original_data != NULL)
579
42.0k
                ok = 1;
580
0
            else
581
0
                ok = 0;
582
42.0k
            break;
583
0
        case 1:
584
0
            if (!ossl_assert(data->running_output != NULL)) {
585
0
                ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
586
0
                ok = 0;
587
0
                break;
588
0
            }
589
590
0
            {
591
                /*
592
                 * Create an object abstraction from the latest output, which
593
                 * was stolen from the previous round.
594
                 */
595
596
0
                OSSL_PARAM *abstract_p = abstract;
597
0
                const char *prev_output_structure =
598
0
                    OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
599
600
0
                *abstract_p++ =
601
0
                    OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
602
0
                                                     (char *)data->data_type, 0);
603
0
                if (prev_output_structure != NULL)
604
0
                    *abstract_p++ =
605
0
                        OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
606
0
                                                         (char *)prev_output_structure,
607
0
                                                         0);
608
0
                *abstract_p++ =
609
0
                    OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
610
0
                                                      data->running_output,
611
0
                                                      data->running_output_length);
612
0
                *abstract_p = OSSL_PARAM_construct_end();
613
0
                current_abstract = abstract;
614
0
            }
615
0
            break;
616
42.0k
        }
617
618
        /* Calling the encoder implementation */
619
620
42.0k
        if (ok) {
621
42.0k
            OSSL_CORE_BIO *cbio = NULL;
622
42.0k
            BIO *current_out = NULL;
623
624
            /*
625
             * If we're at the last encoder instance to use, we're setting up
626
             * final output.  Otherwise, set up an intermediary memory output.
627
             */
628
42.0k
            if (top)
629
42.0k
                current_out = data->bio;
630
0
            else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
631
0
                     == NULL)
632
0
                ok = 0;     /* Assume BIO_new() recorded an error */
633
634
42.0k
            if (ok)
635
42.0k
                ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
636
42.0k
            if (ok) {
637
42.0k
                ok = current_encoder->encode(current_encoder_ctx, cbio,
638
42.0k
                                             original_data, current_abstract,
639
42.0k
                                             data->ctx->selection,
640
42.0k
                                             ossl_pw_passphrase_callback_enc,
641
42.0k
                                             &data->ctx->pwdata);
642
42.0k
                OSSL_TRACE_BEGIN(ENCODER) {
643
0
                    BIO_printf(trc_out,
644
0
                               "[%d] (ctx %p) Running encoder instance %p => %d\n",
645
0
                               data->level, (void *)data->ctx,
646
0
                               (void *)current_encoder_inst, ok);
647
42.0k
                } OSSL_TRACE_END(ENCODER);
648
42.0k
            }
649
650
42.0k
            ossl_core_bio_free(cbio);
651
42.0k
            data->prev_encoder_inst = current_encoder_inst;
652
42.0k
        }
653
42.0k
    }
654
655
    /* Cleanup and collecting the result */
656
657
84.1k
    OPENSSL_free(data->running_output);
658
84.1k
    data->running_output = NULL;
659
660
    /*
661
     * Steal the output from the BIO_s_mem, if we did allocate one.
662
     * That'll be the data for an object abstraction in the next round.
663
     */
664
84.1k
    if (allocated_out != NULL) {
665
0
        BUF_MEM *buf;
666
667
0
        BIO_get_mem_ptr(allocated_out, &buf);
668
0
        data->running_output = (unsigned char *)buf->data;
669
0
        data->running_output_length = buf->length;
670
0
        memset(buf, 0, sizeof(*buf));
671
0
    }
672
673
84.1k
    BIO_free(allocated_out);
674
84.1k
    if (original_data != NULL)
675
42.0k
        data->ctx->cleanup(data->ctx->construct_data);
676
84.1k
    return ok;
677
84.1k
}