Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/encode_decode/encoder_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 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 <ctype.h>
11
12
#include <openssl/core_names.h>
13
#include <openssl/bio.h>
14
#include <openssl/encoder.h>
15
#include <openssl/buffer.h>
16
#include <openssl/params.h>
17
#include <openssl/provider.h>
18
#include <openssl/trace.h>
19
#include <crypto/bn.h>
20
#include "internal/bio.h"
21
#include "internal/ffc.h"
22
#include "internal/provider.h"
23
#include "internal/encoder.h"
24
#include "encoder_local.h"
25
26
/* Number of octets per line */
27
2.15M
#define LABELED_BUF_PRINT_WIDTH 16
28
16.0M
#define LABELED_BN_PRINT_WIDTH 16
29
30
#ifdef SIXTY_FOUR_BIT_LONG
31
#define BN_FMTu "%lu"
32
#define BN_FMTx "%lx"
33
#endif
34
35
#ifdef SIXTY_FOUR_BIT
36
#define BN_FMTu "%llu"
37
#define BN_FMTx "%llx"
38
#endif
39
40
#ifdef THIRTY_TWO_BIT
41
#define BN_FMTu "%u"
42
#define BN_FMTx "%x"
43
#endif
44
45
struct encoder_process_data_st {
46
    OSSL_ENCODER_CTX *ctx;
47
48
    /* Current BIO */
49
    BIO *bio;
50
51
    /* Index of the current encoder instance to be processed */
52
    int current_encoder_inst_index;
53
54
    /* Processing data passed down through recursion */
55
    int level; /* Recursion level */
56
    OSSL_ENCODER_INSTANCE *next_encoder_inst;
57
    int count_output_structure;
58
59
    /* Processing data passed up through recursion */
60
    OSSL_ENCODER_INSTANCE *prev_encoder_inst;
61
    unsigned char *running_output;
62
    size_t running_output_length;
63
    /* Data type = the name of the first succeeding encoder implementation */
64
    const char *data_type;
65
};
66
67
static int encoder_process(struct encoder_process_data_st *data);
68
69
int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
70
84.2k
{
71
84.2k
    struct encoder_process_data_st data;
72
73
84.2k
    memset(&data, 0, sizeof(data));
74
84.2k
    data.ctx = ctx;
75
84.2k
    data.bio = out;
76
84.2k
    data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
77
78
84.2k
    if (data.current_encoder_inst_index == 0) {
79
0
        ERR_raise_data(ERR_LIB_OSSL_ENCODER, OSSL_ENCODER_R_ENCODER_NOT_FOUND,
80
0
            "No encoders were found. For standard encoders you need "
81
0
            "at least one of the default or base providers "
82
0
            "available. Did you forget to load them?");
83
0
        return 0;
84
0
    }
85
86
84.2k
    if (ctx->cleanup == NULL || ctx->construct == NULL) {
87
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INIT_FAIL);
88
0
        return 0;
89
0
    }
90
91
84.2k
    return encoder_process(&data) > 0;
92
84.2k
}
93
94
#ifndef OPENSSL_NO_STDIO
95
static BIO *bio_from_file(FILE *fp)
96
0
{
97
0
    BIO *b;
98
99
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
100
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
101
0
        return NULL;
102
0
    }
103
0
    BIO_set_fp(b, fp, BIO_NOCLOSE);
104
0
    return b;
105
0
}
106
107
int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
108
0
{
109
0
    BIO *b = bio_from_file(fp);
110
0
    int ret = 0;
111
112
0
    if (b != NULL)
113
0
        ret = OSSL_ENCODER_to_bio(ctx, b);
114
115
0
    BIO_free(b);
116
0
    return ret;
117
0
}
118
#endif
119
120
int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
121
    size_t *pdata_len)
122
8.58k
{
123
8.58k
    BIO *out;
124
8.58k
    BUF_MEM *buf = NULL;
125
8.58k
    int ret = 0;
126
127
8.58k
    if (pdata_len == NULL) {
128
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
129
0
        return 0;
130
0
    }
131
132
8.58k
    out = BIO_new(BIO_s_mem());
133
134
8.58k
    if (out != NULL
135
8.58k
        && OSSL_ENCODER_to_bio(ctx, out)
136
6.42k
        && BIO_get_mem_ptr(out, &buf) > 0) {
137
6.42k
        ret = 1; /* Hope for the best. A too small buffer will clear this */
138
139
6.42k
        if (pdata != NULL && *pdata != NULL) {
140
0
            if (*pdata_len < buf->length)
141
                /*
142
                 * It's tempting to do |*pdata_len = (size_t)buf->length|
143
                 * However, it's believed to be confusing more than helpful,
144
                 * so we don't.
145
                 */
146
0
                ret = 0;
147
0
            else
148
0
                *pdata_len -= buf->length;
149
6.42k
        } else {
150
            /* The buffer with the right size is already allocated for us */
151
6.42k
            *pdata_len = (size_t)buf->length;
152
6.42k
        }
153
154
6.42k
        if (ret) {
155
6.42k
            if (pdata != NULL) {
156
6.42k
                if (*pdata != NULL) {
157
0
                    memcpy(*pdata, buf->data, buf->length);
158
0
                    *pdata += buf->length;
159
6.42k
                } else {
160
                    /* In this case, we steal the data from BIO_s_mem() */
161
6.42k
                    *pdata = (unsigned char *)buf->data;
162
6.42k
                    buf->data = NULL;
163
6.42k
                }
164
6.42k
            }
165
6.42k
        }
166
6.42k
    }
167
8.58k
    BIO_free(out);
168
8.58k
    return ret;
169
8.58k
}
170
171
int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
172
12.2k
{
173
12.2k
    if (ctx == NULL) {
174
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
175
0
        return 0;
176
0
    }
177
178
12.2k
    if (ctx->frozen != 0) {
179
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
180
0
        return 0;
181
0
    }
182
183
12.2k
    if (selection == 0) {
184
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
185
0
        return 0;
186
0
    }
187
188
12.2k
    ctx->selection = selection;
189
12.2k
    return 1;
190
12.2k
}
191
192
int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
193
    const char *output_type)
194
12.2k
{
195
12.2k
    if (ctx == NULL || output_type == NULL) {
196
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
197
0
        return 0;
198
0
    }
199
200
12.2k
    if (ctx->frozen != 0) {
201
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
202
0
        return 0;
203
0
    }
204
205
12.2k
    ctx->output_type = output_type;
206
12.2k
    return 1;
207
12.2k
}
208
209
int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
210
    const char *output_structure)
211
0
{
212
0
    if (ctx == NULL || output_structure == NULL) {
213
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
214
0
        return 0;
215
0
    }
216
217
0
    if (ctx->frozen != 0) {
218
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
219
0
        return 0;
220
0
    }
221
222
0
    ctx->output_structure = output_structure;
223
0
    return 1;
224
0
}
225
226
static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
227
    void *encoderctx)
228
93.2k
{
229
93.2k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
230
93.2k
    const OSSL_PROVIDER *prov;
231
93.2k
    OSSL_LIB_CTX *libctx;
232
93.2k
    const OSSL_PROPERTY_LIST *props;
233
93.2k
    const OSSL_PROPERTY_DEFINITION *prop;
234
235
93.2k
    if (encoder == NULL) {
236
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
237
0
        return 0;
238
0
    }
239
240
93.2k
    if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL)
241
0
        return 0;
242
243
93.2k
    if (!OSSL_ENCODER_up_ref(encoder)) {
244
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
245
0
        goto err;
246
0
    }
247
248
93.2k
    prov = OSSL_ENCODER_get0_provider(encoder);
249
93.2k
    libctx = ossl_provider_libctx(prov);
250
93.2k
    props = ossl_encoder_parsed_properties(encoder);
251
93.2k
    if (props == NULL) {
252
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
253
0
            "there are no property definitions with encoder %s",
254
0
            OSSL_ENCODER_get0_name(encoder));
255
0
        goto err;
256
0
    }
257
258
    /* The "output" property is mandatory */
259
93.2k
    prop = ossl_property_find_property(props, libctx, "output");
260
93.2k
    encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
261
93.2k
    if (encoder_inst->output_type == NULL) {
262
0
        ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,
263
0
            "the mandatory 'output' property is missing "
264
0
            "for encoder %s (properties: %s)",
265
0
            OSSL_ENCODER_get0_name(encoder),
266
0
            OSSL_ENCODER_get0_properties(encoder));
267
0
        goto err;
268
0
    }
269
270
    /* The "structure" property is optional */
271
93.2k
    prop = ossl_property_find_property(props, libctx, "structure");
272
93.2k
    if (prop != NULL)
273
68.8k
        encoder_inst->output_structure
274
68.8k
            = ossl_property_get_string_value(libctx, prop);
275
276
93.2k
    encoder_inst->encoder = encoder;
277
93.2k
    encoder_inst->encoderctx = encoderctx;
278
93.2k
    return encoder_inst;
279
0
err:
280
0
    ossl_encoder_instance_free(encoder_inst);
281
0
    return NULL;
282
93.2k
}
283
284
void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
285
649k
{
286
649k
    if (encoder_inst != NULL) {
287
649k
        if (encoder_inst->encoder != NULL)
288
649k
            encoder_inst->encoder->freectx(encoder_inst->encoderctx);
289
649k
        encoder_inst->encoderctx = NULL;
290
649k
        OSSL_ENCODER_free(encoder_inst->encoder);
291
649k
        encoder_inst->encoder = NULL;
292
649k
        OPENSSL_free(encoder_inst);
293
649k
    }
294
649k
}
295
296
static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
297
    OSSL_ENCODER_INSTANCE *ei)
298
649k
{
299
649k
    int ok;
300
301
649k
    if (ctx->encoder_insts == NULL
302
84.2k
        && (ctx->encoder_insts = sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
303
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);
304
0
        return 0;
305
0
    }
306
307
649k
    ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
308
649k
    if (ok) {
309
649k
        OSSL_TRACE_BEGIN(ENCODER)
310
0
        {
311
0
            BIO_printf(trc_out,
312
0
                "(ctx %p) Added encoder instance %p (encoder %p):\n"
313
0
                "    %s with %s\n",
314
0
                (void *)ctx, (void *)ei, (void *)ei->encoder,
315
0
                OSSL_ENCODER_get0_name(ei->encoder),
316
0
                OSSL_ENCODER_get0_properties(ei->encoder));
317
0
        }
318
649k
        OSSL_TRACE_END(ENCODER);
319
649k
    }
320
649k
    return ok;
321
649k
}
322
323
int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
324
93.2k
{
325
93.2k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
326
93.2k
    const OSSL_PROVIDER *prov = NULL;
327
93.2k
    void *encoderctx = NULL;
328
93.2k
    void *provctx = NULL;
329
330
93.2k
    if (ctx == NULL || encoder == NULL) {
331
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
332
0
        return 0;
333
0
    }
334
335
93.2k
    if (ctx->frozen != 0) {
336
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
337
0
        return 0;
338
0
    }
339
340
93.2k
    prov = OSSL_ENCODER_get0_provider(encoder);
341
93.2k
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
342
343
93.2k
    if ((encoderctx = encoder->newctx(provctx)) == NULL
344
93.2k
        || (encoder_inst = ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
345
0
        goto err;
346
    /* Avoid double free of encoderctx on further errors */
347
93.2k
    encoderctx = NULL;
348
349
93.2k
    if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
350
0
        goto err;
351
352
93.2k
    return 1;
353
0
err:
354
0
    ossl_encoder_instance_free(encoder_inst);
355
0
    if (encoderctx != NULL)
356
0
        encoder->freectx(encoderctx);
357
0
    return 0;
358
93.2k
}
359
360
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
361
    OSSL_LIB_CTX *libctx, const char *propq)
362
12.2k
{
363
12.2k
    if (ctx == NULL) {
364
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
365
0
        return 0;
366
0
    }
367
368
12.2k
    if (ctx->frozen != 0) {
369
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
370
0
        return 0;
371
0
    }
372
373
12.2k
    return 1;
374
12.2k
}
375
376
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
377
329k
{
378
329k
    if (ctx == NULL || ctx->encoder_insts == NULL)
379
1.18k
        return 0;
380
328k
    return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
381
329k
}
382
383
int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
384
    OSSL_ENCODER_CONSTRUCT *construct)
385
12.2k
{
386
12.2k
    if (ctx == NULL) {
387
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
388
0
        return 0;
389
0
    }
390
391
12.2k
    if (ctx->frozen != 0) {
392
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
393
0
        return 0;
394
0
    }
395
396
12.2k
    ctx->construct = construct;
397
12.2k
    return 1;
398
12.2k
}
399
400
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
401
    void *construct_data)
402
12.2k
{
403
12.2k
    if (ctx == NULL) {
404
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
405
0
        return 0;
406
0
    }
407
408
12.2k
    if (ctx->frozen != 0) {
409
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
410
0
        return 0;
411
0
    }
412
413
12.2k
    ctx->construct_data = construct_data;
414
12.2k
    return 1;
415
12.2k
}
416
417
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
418
    OSSL_ENCODER_CLEANUP *cleanup)
419
12.2k
{
420
12.2k
    if (ctx == NULL) {
421
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
422
0
        return 0;
423
0
    }
424
425
12.2k
    if (ctx->frozen != 0) {
426
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
427
0
        return 0;
428
0
    }
429
430
12.2k
    ctx->cleanup = cleanup;
431
12.2k
    return 1;
432
12.2k
}
433
434
OSSL_ENCODER *
435
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
436
1.39M
{
437
1.39M
    if (encoder_inst == NULL)
438
0
        return NULL;
439
1.39M
    return encoder_inst->encoder;
440
1.39M
}
441
442
void *
443
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
444
1.29M
{
445
1.29M
    if (encoder_inst == NULL)
446
0
        return NULL;
447
1.29M
    return encoder_inst->encoderctx;
448
1.29M
}
449
450
const char *
451
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
452
649k
{
453
649k
    if (encoder_inst == NULL)
454
0
        return NULL;
455
649k
    return encoder_inst->output_type;
456
649k
}
457
458
const char *
459
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
460
649k
{
461
649k
    if (encoder_inst == NULL)
462
0
        return NULL;
463
649k
    return encoder_inst->output_structure;
464
649k
}
465
466
static int encoder_process(struct encoder_process_data_st *data)
467
168k
{
468
168k
    OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
469
168k
    OSSL_ENCODER *current_encoder = NULL;
470
168k
    OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
471
168k
    BIO *allocated_out = NULL;
472
168k
    const void *original_data = NULL;
473
168k
    OSSL_PARAM abstract[10];
474
168k
    const OSSL_PARAM *current_abstract = NULL;
475
168k
    int i;
476
168k
    int ok = -1; /* -1 signifies that the lookup loop gave nothing */
477
168k
    int top = 0;
478
479
168k
    if (data->next_encoder_inst == NULL) {
480
        /* First iteration, where we prepare for what is to come */
481
482
84.2k
        data->count_output_structure = data->ctx->output_structure == NULL ? -1 : 0;
483
84.2k
        top = 1;
484
84.2k
    }
485
486
733k
    for (i = data->current_encoder_inst_index; i-- > 0;) {
487
649k
        OSSL_ENCODER *next_encoder = NULL;
488
649k
        const char *current_output_type;
489
649k
        const char *current_output_structure;
490
649k
        struct encoder_process_data_st new_data;
491
492
649k
        if (!top)
493
12.6k
            next_encoder = OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
494
495
649k
        current_encoder_inst = sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
496
649k
        current_encoder = OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
497
649k
        current_encoder_ctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
498
649k
        current_output_type = OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
499
649k
        current_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
500
649k
        memset(&new_data, 0, sizeof(new_data));
501
649k
        new_data.ctx = data->ctx;
502
649k
        new_data.current_encoder_inst_index = i;
503
649k
        new_data.next_encoder_inst = current_encoder_inst;
504
649k
        new_data.count_output_structure = data->count_output_structure;
505
649k
        new_data.level = data->level + 1;
506
507
649k
        OSSL_TRACE_BEGIN(ENCODER)
508
0
        {
509
0
            BIO_printf(trc_out,
510
0
                "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
511
0
                data->level, (void *)data->ctx,
512
0
                (void *)current_encoder_inst, (void *)current_encoder);
513
0
        }
514
649k
        OSSL_TRACE_END(ENCODER);
515
516
        /*
517
         * If this is the top call, we check if the output type of the current
518
         * encoder matches the desired output type.
519
         * If this isn't the top call, i.e. this is deeper in the recursion,
520
         * we instead check if the output type of the current encoder matches
521
         * the name of the next encoder (the one found by the parent call).
522
         */
523
649k
        if (top) {
524
636k
            if (data->ctx->output_type != NULL
525
636k
                && OPENSSL_strcasecmp(current_output_type,
526
636k
                       data->ctx->output_type)
527
636k
                    != 0) {
528
524k
                OSSL_TRACE_BEGIN(ENCODER)
529
0
                {
530
0
                    BIO_printf(trc_out,
531
0
                        "[%d]    Skipping because current encoder output type (%s) != desired output type (%s)\n",
532
0
                        data->level,
533
0
                        current_output_type, data->ctx->output_type);
534
0
                }
535
524k
                OSSL_TRACE_END(ENCODER);
536
524k
                continue;
537
524k
            }
538
636k
        } else {
539
12.6k
            if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
540
12.6k
                OSSL_TRACE_BEGIN(ENCODER)
541
0
                {
542
0
                    BIO_printf(trc_out,
543
0
                        "[%d]    Skipping because current encoder output type (%s) != name of encoder %p\n",
544
0
                        data->level,
545
0
                        current_output_type, (void *)next_encoder);
546
0
                }
547
12.6k
                OSSL_TRACE_END(ENCODER);
548
12.6k
                continue;
549
12.6k
            }
550
12.6k
        }
551
552
        /*
553
         * If the caller and the current encoder specify an output structure,
554
         * Check if they match.  If they do, count the match, otherwise skip
555
         * the current encoder.
556
         */
557
112k
        if (data->ctx->output_structure != NULL
558
37.2k
            && current_output_structure != NULL) {
559
37.2k
            if (OPENSSL_strcasecmp(data->ctx->output_structure,
560
37.2k
                    current_output_structure)
561
37.2k
                != 0) {
562
28.8k
                OSSL_TRACE_BEGIN(ENCODER)
563
0
                {
564
0
                    BIO_printf(trc_out,
565
0
                        "[%d]    Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
566
0
                        data->level,
567
0
                        current_output_structure,
568
0
                        data->ctx->output_structure);
569
0
                }
570
28.8k
                OSSL_TRACE_END(ENCODER);
571
28.8k
                continue;
572
28.8k
            }
573
574
8.38k
            data->count_output_structure++;
575
8.38k
        }
576
577
        /*
578
         * Recurse to process the encoder implementations before the current
579
         * one.
580
         */
581
84.0k
        ok = encoder_process(&new_data);
582
583
84.0k
        data->prev_encoder_inst = new_data.prev_encoder_inst;
584
84.0k
        data->running_output = new_data.running_output;
585
84.0k
        data->running_output_length = new_data.running_output_length;
586
587
        /*
588
         * ok == -1     means that the recursion call above gave no further
589
         *              encoders, and that the one we're currently at should
590
         *              be tried.
591
         * ok == 0      means that something failed in the recursion call
592
         *              above, making the result unsuitable for a chain.
593
         *              In this case, we simply continue to try finding a
594
         *              suitable encoder at this recursion level.
595
         * ok == 1      means that the recursion call was successful, and we
596
         *              try to use the result at this recursion level.
597
         */
598
84.0k
        if (ok != 0)
599
84.0k
            break;
600
601
0
        OSSL_TRACE_BEGIN(ENCODER)
602
0
        {
603
0
            BIO_printf(trc_out,
604
0
                "[%d]    Skipping because recursion level %d failed\n",
605
0
                data->level, new_data.level);
606
0
        }
607
0
        OSSL_TRACE_END(ENCODER);
608
0
    }
609
610
    /*
611
     * If |i < 0|, we didn't find any useful encoder in this recursion, so
612
     * we do the rest of the process only if |i >= 0|.
613
     */
614
168k
    if (i < 0) {
615
84.2k
        ok = -1;
616
617
84.2k
        OSSL_TRACE_BEGIN(ENCODER)
618
0
        {
619
0
            BIO_printf(trc_out,
620
0
                "[%d] (ctx %p) No suitable encoder found\n",
621
0
                data->level, (void *)data->ctx);
622
0
        }
623
84.2k
        OSSL_TRACE_END(ENCODER);
624
84.2k
    } else {
625
        /* Preparations */
626
627
84.0k
        switch (ok) {
628
0
        case 0:
629
0
            break;
630
84.0k
        case -1:
631
            /*
632
             * We have reached the beginning of the encoder instance sequence,
633
             * so we prepare the object to be encoded.
634
             */
635
636
            /*
637
             * |data->count_output_structure| is one of these values:
638
             *
639
             * -1       There is no desired output structure
640
             *  0       There is a desired output structure, and it wasn't
641
             *          matched by any of the encoder instances that were
642
             *          considered
643
             * >0       There is a desired output structure, and at least one
644
             *          of the encoder instances matched it
645
             */
646
84.0k
            if (data->count_output_structure == 0)
647
0
                return 0;
648
649
84.0k
            original_data = data->ctx->construct(current_encoder_inst,
650
84.0k
                data->ctx->construct_data);
651
652
            /* Also set the data type, using the encoder implementation name */
653
84.0k
            data->data_type = OSSL_ENCODER_get0_name(current_encoder);
654
655
            /* Assume that the constructor recorded an error */
656
84.0k
            if (original_data != NULL)
657
84.0k
                ok = 1;
658
0
            else
659
0
                ok = 0;
660
84.0k
            break;
661
0
        case 1:
662
0
            if (!ossl_assert(data->running_output != NULL)) {
663
0
                ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
664
0
                ok = 0;
665
0
                break;
666
0
            }
667
668
0
            {
669
                /*
670
                 * Create an object abstraction from the latest output, which
671
                 * was stolen from the previous round.
672
                 */
673
674
0
                OSSL_PARAM *abstract_p = abstract;
675
0
                const char *prev_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
676
677
0
                *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
678
0
                    (char *)data->data_type, 0);
679
0
                if (prev_output_structure != NULL)
680
0
                    *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
681
0
                        (char *)prev_output_structure,
682
0
                        0);
683
0
                *abstract_p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
684
0
                    data->running_output,
685
0
                    data->running_output_length);
686
0
                *abstract_p = OSSL_PARAM_construct_end();
687
0
                current_abstract = abstract;
688
0
            }
689
0
            break;
690
84.0k
        }
691
692
        /* Calling the encoder implementation */
693
694
84.0k
        if (ok) {
695
84.0k
            OSSL_CORE_BIO *cbio = NULL;
696
84.0k
            BIO *current_out = NULL;
697
698
            /*
699
             * If we're at the last encoder instance to use, we're setting up
700
             * final output.  Otherwise, set up an intermediary memory output.
701
             */
702
84.0k
            if (top)
703
84.0k
                current_out = data->bio;
704
0
            else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
705
0
                == NULL)
706
0
                ok = 0; /* Assume BIO_new() recorded an error */
707
708
84.0k
            if (ok)
709
84.0k
                ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
710
84.0k
            if (ok) {
711
84.0k
                ok = current_encoder->encode(current_encoder_ctx, cbio,
712
84.0k
                    original_data, current_abstract,
713
84.0k
                    data->ctx->selection,
714
84.0k
                    ossl_pw_passphrase_callback_enc,
715
84.0k
                    &data->ctx->pwdata);
716
84.0k
                OSSL_TRACE_BEGIN(ENCODER)
717
0
                {
718
0
                    BIO_printf(trc_out,
719
0
                        "[%d] (ctx %p) Running encoder instance %p => %d\n",
720
0
                        data->level, (void *)data->ctx,
721
0
                        (void *)current_encoder_inst, ok);
722
0
                }
723
84.0k
                OSSL_TRACE_END(ENCODER);
724
84.0k
            }
725
726
84.0k
            ossl_core_bio_free(cbio);
727
84.0k
            data->prev_encoder_inst = current_encoder_inst;
728
84.0k
        }
729
84.0k
    }
730
731
    /* Cleanup and collecting the result */
732
733
168k
    OPENSSL_free(data->running_output);
734
168k
    data->running_output = NULL;
735
736
    /*
737
     * Steal the output from the BIO_s_mem, if we did allocate one.
738
     * That'll be the data for an object abstraction in the next round.
739
     */
740
168k
    if (allocated_out != NULL) {
741
0
        BUF_MEM *buf;
742
743
0
        BIO_get_mem_ptr(allocated_out, &buf);
744
0
        data->running_output = (unsigned char *)buf->data;
745
0
        data->running_output_length = buf->length;
746
0
        memset(buf, 0, sizeof(*buf));
747
0
    }
748
749
168k
    BIO_free(allocated_out);
750
168k
    if (original_data != NULL)
751
84.0k
        data->ctx->cleanup(data->ctx->construct_data);
752
168k
    return ok;
753
168k
}
754
755
int ossl_bio_print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)
756
24.3k
{
757
24.3k
    int ret = 0, use_sep = 0;
758
24.3k
    char *hex_str = NULL, *p;
759
24.3k
    const char spaces[] = "    ";
760
24.3k
    const char *post_label_spc = " ";
761
762
24.3k
    const char *neg = "";
763
24.3k
    int bytes;
764
765
24.3k
    if (bn == NULL)
766
509
        return 0;
767
23.8k
    if (label == NULL) {
768
1.78k
        label = "";
769
1.78k
        post_label_spc = "";
770
1.78k
    }
771
772
23.8k
    if (BN_is_zero(bn))
773
1.18k
        return BIO_printf(out, "%s%s0\n", label, post_label_spc);
774
775
22.6k
    if (BN_num_bytes(bn) <= BN_BYTES) {
776
9.37k
        BN_ULONG *words = bn_get_words(bn);
777
778
9.37k
        if (BN_is_negative(bn))
779
26
            neg = "-";
780
781
9.37k
        return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
782
9.37k
            label, post_label_spc, neg, words[0], neg, words[0]);
783
9.37k
    }
784
785
13.3k
    hex_str = BN_bn2hex(bn);
786
13.3k
    if (hex_str == NULL)
787
0
        return 0;
788
789
13.3k
    p = hex_str;
790
13.3k
    if (*p == '-') {
791
16
        ++p;
792
16
        neg = " (Negative)";
793
16
    }
794
13.3k
    if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
795
0
        goto err;
796
797
    /* Keep track of how many bytes we have printed out so far */
798
13.3k
    bytes = 0;
799
800
13.3k
    if (BIO_printf(out, "%s", spaces) <= 0)
801
0
        goto err;
802
803
16.0M
    while (*p != '\0') {
804
        /* Do a newline after every n hex bytes + add the space indent */
805
16.0M
        if ((bytes % LABELED_BN_PRINT_WIDTH) == 0 && bytes > 0) {
806
993k
            if (BIO_printf(out, ":\n%s", spaces) <= 0)
807
0
                goto err;
808
993k
            use_sep = 0; /* The first byte on the next line doesn't have a : */
809
993k
        }
810
16.0M
        if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
811
16.0M
                tolower((unsigned char)p[0]),
812
16.0M
                tolower((unsigned char)p[1]))
813
16.0M
            <= 0)
814
0
            goto err;
815
16.0M
        ++bytes;
816
16.0M
        p += 2;
817
16.0M
        use_sep = 1;
818
16.0M
    }
819
13.3k
    if (BIO_printf(out, "\n") <= 0)
820
0
        goto err;
821
13.3k
    ret = 1;
822
13.3k
err:
823
13.3k
    OPENSSL_free(hex_str);
824
13.3k
    return ret;
825
13.3k
}
826
827
int ossl_bio_print_labeled_buf(BIO *out, const char *label,
828
    const unsigned char *buf, size_t buflen)
829
10.9k
{
830
10.9k
    size_t i;
831
832
10.9k
    if (BIO_printf(out, "%s\n", label) <= 0)
833
0
        return 0;
834
835
2.17M
    for (i = 0; i < buflen; i++) {
836
2.15M
        if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
837
145k
            if (i > 0 && BIO_printf(out, "\n") <= 0)
838
0
                return 0;
839
145k
            if (BIO_printf(out, "    ") <= 0)
840
0
                return 0;
841
145k
        }
842
843
2.15M
        if (BIO_printf(out, "%02x%s", buf[i],
844
2.15M
                (i == buflen - 1) ? "" : ":")
845
2.15M
            <= 0)
846
0
            return 0;
847
2.15M
    }
848
10.9k
    if (BIO_printf(out, "\n") <= 0)
849
0
        return 0;
850
851
10.9k
    return 1;
852
10.9k
}
853
854
#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
855
int ossl_bio_print_ffc_params(BIO *out, const FFC_PARAMS *ffc)
856
12.2k
{
857
12.2k
    if (ffc->nid != NID_undef) {
858
0
#ifndef OPENSSL_NO_DH
859
0
        const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
860
0
        const char *name = ossl_ffc_named_group_get_name(group);
861
862
0
        if (name == NULL)
863
0
            goto err;
864
0
        if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
865
0
            goto err;
866
0
        return 1;
867
#else
868
        /* How could this be? We should not have a nid in a no-dh build. */
869
        goto err;
870
#endif
871
0
    }
872
873
12.2k
    if (!ossl_bio_print_labeled_bignum(out, "P:   ", ffc->p))
874
0
        goto err;
875
12.2k
    if (ffc->q != NULL) {
876
10.1k
        if (!ossl_bio_print_labeled_bignum(out, "Q:   ", ffc->q))
877
0
            goto err;
878
10.1k
    }
879
12.2k
    if (!ossl_bio_print_labeled_bignum(out, "G:   ", ffc->g))
880
0
        goto err;
881
12.2k
    if (ffc->j != NULL) {
882
84
        if (!ossl_bio_print_labeled_bignum(out, "J:   ", ffc->j))
883
0
            goto err;
884
84
    }
885
12.2k
    if (ffc->seed != NULL) {
886
127
        if (!ossl_bio_print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
887
0
            goto err;
888
127
    }
889
12.2k
    if (ffc->gindex != -1) {
890
0
        if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
891
0
            goto err;
892
0
    }
893
12.2k
    if (ffc->pcounter != -1) {
894
157
        if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
895
0
            goto err;
896
157
    }
897
12.2k
    if (ffc->h != 0) {
898
0
        if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
899
0
            goto err;
900
0
    }
901
12.2k
    return 1;
902
0
err:
903
0
    return 0;
904
12.2k
}
905
906
#endif