Coverage Report

Created: 2023-06-08 06:43

/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
5.71k
{
47
5.71k
    struct encoder_process_data_st data;
48
49
5.71k
    memset(&data, 0, sizeof(data));
50
5.71k
    data.ctx = ctx;
51
5.71k
    data.bio = out;
52
5.71k
    data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
53
54
5.71k
    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
5.71k
    return encoder_process(&data) > 0;
63
5.71k
}
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.68k
{
94
2.68k
    BIO *out;
95
2.68k
    BUF_MEM *buf = NULL;
96
2.68k
    int ret = 0;
97
98
2.68k
    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.68k
    out = BIO_new(BIO_s_mem());
104
105
2.68k
    if (out != NULL
106
2.68k
        && OSSL_ENCODER_to_bio(ctx, out)
107
2.68k
        && BIO_get_mem_ptr(out, &buf) > 0) {
108
2.08k
        ret = 1; /* Hope for the best. A too small buffer will clear this */
109
110
2.08k
        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.08k
        } else {
121
            /* The buffer with the right size is already allocated for us */
122
2.08k
            *pdata_len = (size_t)buf->length;
123
2.08k
        }
124
125
2.08k
        if (ret) {
126
2.08k
            if (pdata != NULL) {
127
2.08k
                if (*pdata != NULL) {
128
0
                    memcpy(*pdata, buf->data, buf->length);
129
0
                    *pdata += buf->length;
130
2.08k
                } else {
131
                    /* In this case, we steal the data from BIO_s_mem() */
132
2.08k
                    *pdata = (unsigned char *)buf->data;
133
2.08k
                    buf->data = NULL;
134
2.08k
                }
135
2.08k
            }
136
2.08k
        }
137
2.08k
    }
138
2.68k
    BIO_free(out);
139
2.68k
    return ret;
140
2.68k
}
141
142
int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
143
5.84k
{
144
5.84k
    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
5.84k
    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
5.84k
    ctx->selection = selection;
155
5.84k
    return 1;
156
5.84k
}
157
158
int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
159
                                     const char *output_type)
160
5.84k
{
161
5.84k
    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
5.84k
    ctx->output_type = output_type;
167
5.84k
    return 1;
168
5.84k
}
169
170
int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
171
                                          const char *output_structure)
172
2.68k
{
173
2.68k
    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.68k
    ctx->output_structure = output_structure;
179
2.68k
    return 1;
180
2.68k
}
181
182
static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
183
                                                        void *encoderctx)
184
62.3k
{
185
62.3k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
186
62.3k
    const OSSL_PROVIDER *prov;
187
62.3k
    OSSL_LIB_CTX *libctx;
188
62.3k
    const OSSL_PROPERTY_LIST *props;
189
62.3k
    const OSSL_PROPERTY_DEFINITION *prop;
190
191
62.3k
    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
62.3k
    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
62.3k
    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
62.3k
    prov = OSSL_ENCODER_get0_provider(encoder);
207
62.3k
    libctx = ossl_provider_libctx(prov);
208
62.3k
    props = ossl_encoder_parsed_properties(encoder);
209
62.3k
    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
62.3k
    prop = ossl_property_find_property(props, libctx, "output");
218
62.3k
    encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
219
62.3k
    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
62.3k
    prop = ossl_property_find_property(props, libctx, "structure");
230
62.3k
    if (prop != NULL)
231
50.2k
        encoder_inst->output_structure
232
50.2k
            = ossl_property_get_string_value(libctx, prop);
233
234
62.3k
    encoder_inst->encoder = encoder;
235
62.3k
    encoder_inst->encoderctx = encoderctx;
236
62.3k
    return encoder_inst;
237
0
 err:
238
0
    ossl_encoder_instance_free(encoder_inst);
239
0
    return NULL;
240
62.3k
}
241
242
void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
243
62.3k
{
244
62.3k
    if (encoder_inst != NULL) {
245
62.3k
        if (encoder_inst->encoder != NULL)
246
62.3k
            encoder_inst->encoder->freectx(encoder_inst->encoderctx);
247
62.3k
        encoder_inst->encoderctx = NULL;
248
62.3k
        OSSL_ENCODER_free(encoder_inst->encoder);
249
62.3k
        encoder_inst->encoder = NULL;
250
62.3k
        OPENSSL_free(encoder_inst);
251
62.3k
    }
252
62.3k
}
253
254
static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
255
                                             OSSL_ENCODER_INSTANCE *ei)
256
62.3k
{
257
62.3k
    int ok;
258
259
62.3k
    if (ctx->encoder_insts == NULL
260
62.3k
        && (ctx->encoder_insts =
261
5.71k
            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
62.3k
    ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
267
62.3k
    if (ok) {
268
62.3k
        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
62.3k
        } OSSL_TRACE_END(ENCODER);
276
62.3k
    }
277
62.3k
    return ok;
278
62.3k
}
279
280
int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
281
62.3k
{
282
62.3k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
283
62.3k
    const OSSL_PROVIDER *prov = NULL;
284
62.3k
    void *encoderctx = NULL;
285
62.3k
    void *provctx = NULL;
286
287
62.3k
    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
62.3k
    prov = OSSL_ENCODER_get0_provider(encoder);
293
62.3k
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
294
295
62.3k
    if ((encoderctx = encoder->newctx(provctx)) == NULL
296
62.3k
        || (encoder_inst =
297
62.3k
            ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
298
0
        goto err;
299
    /* Avoid double free of encoderctx on further errors */
300
62.3k
    encoderctx = NULL;
301
302
62.3k
    if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
303
0
        goto err;
304
305
62.3k
    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
62.3k
}
312
313
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
314
                               OSSL_LIB_CTX *libctx, const char *propq)
315
5.84k
{
316
5.84k
    return 1;
317
5.84k
}
318
319
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
320
20.3k
{
321
20.3k
    if (ctx == NULL || ctx->encoder_insts == NULL)
322
126
        return 0;
323
20.1k
    return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
324
20.3k
}
325
326
int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
327
                                   OSSL_ENCODER_CONSTRUCT *construct)
328
5.71k
{
329
5.71k
    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
5.71k
    ctx->construct = construct;
334
5.71k
    return 1;
335
5.71k
}
336
337
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
338
                                        void *construct_data)
339
5.71k
{
340
5.71k
    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
5.71k
    ctx->construct_data = construct_data;
345
5.71k
    return 1;
346
5.71k
}
347
348
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
349
                                 OSSL_ENCODER_CLEANUP *cleanup)
350
5.71k
{
351
5.71k
    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
5.71k
    ctx->cleanup = cleanup;
356
5.71k
    return 1;
357
5.71k
}
358
359
OSSL_ENCODER *
360
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
361
134k
{
362
134k
    if (encoder_inst == NULL)
363
0
        return NULL;
364
134k
    return encoder_inst->encoder;
365
134k
}
366
367
void *
368
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
369
124k
{
370
124k
    if (encoder_inst == NULL)
371
0
        return NULL;
372
124k
    return encoder_inst->encoderctx;
373
124k
}
374
375
const char *
376
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
377
62.3k
{
378
62.3k
    if (encoder_inst == NULL)
379
0
        return NULL;
380
62.3k
    return encoder_inst->output_type;
381
62.3k
}
382
383
const char *
384
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
385
62.3k
{
386
62.3k
    if (encoder_inst == NULL)
387
0
        return NULL;
388
62.3k
    return encoder_inst->output_structure;
389
62.3k
}
390
391
static int encoder_process(struct encoder_process_data_st *data)
392
11.3k
{
393
11.3k
    OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
394
11.3k
    OSSL_ENCODER *current_encoder = NULL;
395
11.3k
    OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
396
11.3k
    BIO *allocated_out = NULL;
397
11.3k
    const void *original_data = NULL;
398
11.3k
    OSSL_PARAM abstract[10];
399
11.3k
    const OSSL_PARAM *current_abstract = NULL;
400
11.3k
    int i;
401
11.3k
    int ok = -1;  /* -1 signifies that the lookup loop gave nothing */
402
11.3k
    int top = 0;
403
404
11.3k
    if (data->next_encoder_inst == NULL) {
405
        /* First iteration, where we prepare for what is to come */
406
407
5.71k
        data->count_output_structure =
408
5.71k
            data->ctx->output_structure == NULL ? -1 : 0;
409
5.71k
        top = 1;
410
5.71k
    }
411
412
68.1k
    for (i = data->current_encoder_inst_index; i-- > 0;) {
413
62.3k
        OSSL_ENCODER *next_encoder = NULL;
414
62.3k
        const char *current_output_type;
415
62.3k
        const char *current_output_structure;
416
62.3k
        struct encoder_process_data_st new_data;
417
418
62.3k
        if (!top)
419
3.85k
            next_encoder =
420
3.85k
                OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
421
422
62.3k
        current_encoder_inst =
423
62.3k
            sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
424
62.3k
        current_encoder =
425
62.3k
            OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
426
62.3k
        current_encoder_ctx =
427
62.3k
            OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
428
62.3k
        current_output_type =
429
62.3k
            OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
430
62.3k
        current_output_structure =
431
62.3k
            OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
432
62.3k
        memset(&new_data, 0, sizeof(new_data));
433
62.3k
        new_data.ctx = data->ctx;
434
62.3k
        new_data.current_encoder_inst_index = i;
435
62.3k
        new_data.next_encoder_inst = current_encoder_inst;
436
62.3k
        new_data.count_output_structure = data->count_output_structure;
437
62.3k
        new_data.level = data->level + 1;
438
439
62.3k
        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
62.3k
        } 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
62.3k
        if (top) {
454
58.5k
            if (data->ctx->output_type != NULL
455
58.5k
                && OPENSSL_strcasecmp(current_output_type,
456
58.5k
                                      data->ctx->output_type) != 0) {
457
43.4k
                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
43.4k
                } OSSL_TRACE_END(ENCODER);
463
43.4k
                continue;
464
43.4k
            }
465
58.5k
        } else {
466
3.85k
            if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
467
3.85k
                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.85k
                } OSSL_TRACE_END(ENCODER);
473
3.85k
                continue;
474
3.85k
            }
475
3.85k
        }
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
15.1k
        if (data->ctx->output_structure != NULL
483
15.1k
            && current_output_structure != NULL) {
484
12.0k
            if (OPENSSL_strcasecmp(data->ctx->output_structure,
485
12.0k
                                   current_output_structure) != 0) {
486
9.43k
                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.43k
                } OSSL_TRACE_END(ENCODER);
493
9.43k
                continue;
494
9.43k
            }
495
496
2.65k
            data->count_output_structure++;
497
2.65k
        }
498
499
        /*
500
         * Recurse to process the encoder implementations before the current
501
         * one.
502
         */
503
5.68k
        ok = encoder_process(&new_data);
504
505
5.68k
        data->prev_encoder_inst = new_data.prev_encoder_inst;
506
5.68k
        data->running_output = new_data.running_output;
507
5.68k
        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
5.68k
        if (ok != 0)
521
5.68k
            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
11.3k
    if (i < 0) {
535
5.71k
        ok = -1;
536
537
5.71k
        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
5.71k
        } OSSL_TRACE_END(ENCODER);
542
5.71k
    } else {
543
        /* Preparations */
544
545
5.68k
        switch (ok) {
546
0
        case 0:
547
0
            break;
548
5.68k
        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
5.68k
            if (data->count_output_structure == 0)
565
0
                return 0;
566
567
5.68k
            original_data =
568
5.68k
                data->ctx->construct(current_encoder_inst,
569
5.68k
                                     data->ctx->construct_data);
570
571
            /* Also set the data type, using the encoder implementation name */
572
5.68k
            data->data_type = OSSL_ENCODER_get0_name(current_encoder);
573
574
            /* Assume that the constructor recorded an error */
575
5.68k
            if (original_data != NULL)
576
5.68k
                ok = 1;
577
0
            else
578
0
                ok = 0;
579
5.68k
            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
5.68k
        }
614
615
        /* Calling the encoder implementation */
616
617
5.68k
        if (ok) {
618
5.68k
            OSSL_CORE_BIO *cbio = NULL;
619
5.68k
            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
5.68k
            if (top)
626
5.68k
                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
5.68k
            if (ok)
632
5.68k
                ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
633
5.68k
            if (ok) {
634
5.68k
                ok = current_encoder->encode(current_encoder_ctx, cbio,
635
5.68k
                                             original_data, current_abstract,
636
5.68k
                                             data->ctx->selection,
637
5.68k
                                             ossl_pw_passphrase_callback_enc,
638
5.68k
                                             &data->ctx->pwdata);
639
5.68k
                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
5.68k
                } OSSL_TRACE_END(ENCODER);
645
5.68k
            }
646
647
5.68k
            ossl_core_bio_free(cbio);
648
5.68k
            data->prev_encoder_inst = current_encoder_inst;
649
5.68k
        }
650
5.68k
    }
651
652
    /* Cleanup and collecting the result */
653
654
11.3k
    OPENSSL_free(data->running_output);
655
11.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
11.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
11.3k
    BIO_free(allocated_out);
671
11.3k
    if (original_data != NULL)
672
5.68k
        data->ctx->cleanup(data->ctx->construct_data);
673
11.3k
    return ok;
674
11.3k
}