Coverage Report

Created: 2025-12-10 06:24

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
0
#define LABELED_BUF_PRINT_WIDTH 16
28
0
#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
0
{
71
0
    struct encoder_process_data_st data;
72
73
0
    memset(&data, 0, sizeof(data));
74
0
    data.ctx = ctx;
75
0
    data.bio = out;
76
0
    data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
77
78
0
    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
0
    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
0
    return encoder_process(&data) > 0;
92
0
}
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
0
{
123
0
    BIO *out;
124
0
    BUF_MEM *buf = NULL;
125
0
    int ret = 0;
126
127
0
    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
0
    out = BIO_new(BIO_s_mem());
133
134
0
    if (out != NULL
135
0
        && OSSL_ENCODER_to_bio(ctx, out)
136
0
        && BIO_get_mem_ptr(out, &buf) > 0) {
137
0
        ret = 1; /* Hope for the best. A too small buffer will clear this */
138
139
0
        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
0
        } else {
150
            /* The buffer with the right size is already allocated for us */
151
0
            *pdata_len = (size_t)buf->length;
152
0
        }
153
154
0
        if (ret) {
155
0
            if (pdata != NULL) {
156
0
                if (*pdata != NULL) {
157
0
                    memcpy(*pdata, buf->data, buf->length);
158
0
                    *pdata += buf->length;
159
0
                } else {
160
                    /* In this case, we steal the data from BIO_s_mem() */
161
0
                    *pdata = (unsigned char *)buf->data;
162
0
                    buf->data = NULL;
163
0
                }
164
0
            }
165
0
        }
166
0
    }
167
0
    BIO_free(out);
168
0
    return ret;
169
0
}
170
171
int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
172
0
{
173
0
    if (ctx == NULL) {
174
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
175
0
        return 0;
176
0
    }
177
178
0
    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
0
    if (selection == 0) {
184
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
185
0
        return 0;
186
0
    }
187
188
0
    ctx->selection = selection;
189
0
    return 1;
190
0
}
191
192
int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
193
    const char *output_type)
194
0
{
195
0
    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
0
    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
0
    ctx->output_type = output_type;
206
0
    return 1;
207
0
}
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
0
{
229
0
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
230
0
    const OSSL_PROVIDER *prov;
231
0
    OSSL_LIB_CTX *libctx;
232
0
    const OSSL_PROPERTY_LIST *props;
233
0
    const OSSL_PROPERTY_DEFINITION *prop;
234
235
0
    if (encoder == NULL) {
236
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
237
0
        return 0;
238
0
    }
239
240
0
    if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL)
241
0
        return 0;
242
243
0
    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
0
    prov = OSSL_ENCODER_get0_provider(encoder);
249
0
    libctx = ossl_provider_libctx(prov);
250
0
    props = ossl_encoder_parsed_properties(encoder);
251
0
    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
0
    prop = ossl_property_find_property(props, libctx, "output");
260
0
    encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
261
0
    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
0
    prop = ossl_property_find_property(props, libctx, "structure");
272
0
    if (prop != NULL)
273
0
        encoder_inst->output_structure
274
0
            = ossl_property_get_string_value(libctx, prop);
275
276
0
    encoder_inst->encoder = encoder;
277
0
    encoder_inst->encoderctx = encoderctx;
278
0
    return encoder_inst;
279
0
err:
280
0
    ossl_encoder_instance_free(encoder_inst);
281
0
    return NULL;
282
0
}
283
284
void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
285
0
{
286
0
    if (encoder_inst != NULL) {
287
0
        if (encoder_inst->encoder != NULL)
288
0
            encoder_inst->encoder->freectx(encoder_inst->encoderctx);
289
0
        encoder_inst->encoderctx = NULL;
290
0
        OSSL_ENCODER_free(encoder_inst->encoder);
291
0
        encoder_inst->encoder = NULL;
292
0
        OPENSSL_free(encoder_inst);
293
0
    }
294
0
}
295
296
static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
297
    OSSL_ENCODER_INSTANCE *ei)
298
0
{
299
0
    int ok;
300
301
0
    if (ctx->encoder_insts == NULL
302
0
        && (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
0
    ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
308
0
    if (ok) {
309
0
        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
0
        OSSL_TRACE_END(ENCODER);
319
0
    }
320
0
    return ok;
321
0
}
322
323
int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
324
0
{
325
0
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
326
0
    const OSSL_PROVIDER *prov = NULL;
327
0
    void *encoderctx = NULL;
328
0
    void *provctx = NULL;
329
330
0
    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
0
    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
0
    prov = OSSL_ENCODER_get0_provider(encoder);
341
0
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
342
343
0
    if ((encoderctx = encoder->newctx(provctx)) == NULL
344
0
        || (encoder_inst = ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
345
0
        goto err;
346
    /* Avoid double free of encoderctx on further errors */
347
0
    encoderctx = NULL;
348
349
0
    if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
350
0
        goto err;
351
352
0
    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
0
}
359
360
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
361
    OSSL_LIB_CTX *libctx, const char *propq)
362
0
{
363
0
    if (ctx == NULL) {
364
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
365
0
        return 0;
366
0
    }
367
368
0
    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
0
    return 1;
374
0
}
375
376
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
377
0
{
378
0
    if (ctx == NULL || ctx->encoder_insts == NULL)
379
0
        return 0;
380
0
    return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
381
0
}
382
383
int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
384
    OSSL_ENCODER_CONSTRUCT *construct)
385
0
{
386
0
    if (ctx == NULL) {
387
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
388
0
        return 0;
389
0
    }
390
391
0
    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
0
    ctx->construct = construct;
397
0
    return 1;
398
0
}
399
400
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
401
    void *construct_data)
402
0
{
403
0
    if (ctx == NULL) {
404
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
405
0
        return 0;
406
0
    }
407
408
0
    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
0
    ctx->construct_data = construct_data;
414
0
    return 1;
415
0
}
416
417
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
418
    OSSL_ENCODER_CLEANUP *cleanup)
419
0
{
420
0
    if (ctx == NULL) {
421
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
422
0
        return 0;
423
0
    }
424
425
0
    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
0
    ctx->cleanup = cleanup;
431
0
    return 1;
432
0
}
433
434
OSSL_ENCODER *
435
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
436
0
{
437
0
    if (encoder_inst == NULL)
438
0
        return NULL;
439
0
    return encoder_inst->encoder;
440
0
}
441
442
void *
443
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
444
0
{
445
0
    if (encoder_inst == NULL)
446
0
        return NULL;
447
0
    return encoder_inst->encoderctx;
448
0
}
449
450
const char *
451
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
452
0
{
453
0
    if (encoder_inst == NULL)
454
0
        return NULL;
455
0
    return encoder_inst->output_type;
456
0
}
457
458
const char *
459
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
460
0
{
461
0
    if (encoder_inst == NULL)
462
0
        return NULL;
463
0
    return encoder_inst->output_structure;
464
0
}
465
466
static int encoder_process(struct encoder_process_data_st *data)
467
0
{
468
0
    OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
469
0
    OSSL_ENCODER *current_encoder = NULL;
470
0
    OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
471
0
    BIO *allocated_out = NULL;
472
0
    const void *original_data = NULL;
473
0
    OSSL_PARAM abstract[10];
474
0
    const OSSL_PARAM *current_abstract = NULL;
475
0
    int i;
476
0
    int ok = -1; /* -1 signifies that the lookup loop gave nothing */
477
0
    int top = 0;
478
479
0
    if (data->next_encoder_inst == NULL) {
480
        /* First iteration, where we prepare for what is to come */
481
482
0
        data->count_output_structure = data->ctx->output_structure == NULL ? -1 : 0;
483
0
        top = 1;
484
0
    }
485
486
0
    for (i = data->current_encoder_inst_index; i-- > 0;) {
487
0
        OSSL_ENCODER *next_encoder = NULL;
488
0
        const char *current_output_type;
489
0
        const char *current_output_structure;
490
0
        struct encoder_process_data_st new_data;
491
492
0
        if (!top)
493
0
            next_encoder = OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
494
495
0
        current_encoder_inst = sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
496
0
        current_encoder = OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
497
0
        current_encoder_ctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
498
0
        current_output_type = OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
499
0
        current_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
500
0
        memset(&new_data, 0, sizeof(new_data));
501
0
        new_data.ctx = data->ctx;
502
0
        new_data.current_encoder_inst_index = i;
503
0
        new_data.next_encoder_inst = current_encoder_inst;
504
0
        new_data.count_output_structure = data->count_output_structure;
505
0
        new_data.level = data->level + 1;
506
507
0
        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
0
        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
0
        if (top) {
524
0
            if (data->ctx->output_type != NULL
525
0
                && OPENSSL_strcasecmp(current_output_type,
526
0
                       data->ctx->output_type)
527
0
                    != 0) {
528
0
                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
0
                OSSL_TRACE_END(ENCODER);
536
0
                continue;
537
0
            }
538
0
        } else {
539
0
            if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
540
0
                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
0
                OSSL_TRACE_END(ENCODER);
548
0
                continue;
549
0
            }
550
0
        }
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
0
        if (data->ctx->output_structure != NULL
558
0
            && current_output_structure != NULL) {
559
0
            if (OPENSSL_strcasecmp(data->ctx->output_structure,
560
0
                    current_output_structure)
561
0
                != 0) {
562
0
                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
0
                OSSL_TRACE_END(ENCODER);
571
0
                continue;
572
0
            }
573
574
0
            data->count_output_structure++;
575
0
        }
576
577
        /*
578
         * Recurse to process the encoder implementations before the current
579
         * one.
580
         */
581
0
        ok = encoder_process(&new_data);
582
583
0
        data->prev_encoder_inst = new_data.prev_encoder_inst;
584
0
        data->running_output = new_data.running_output;
585
0
        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
0
        if (ok != 0)
599
0
            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
0
    if (i < 0) {
615
0
        ok = -1;
616
617
0
        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
0
        OSSL_TRACE_END(ENCODER);
624
0
    } else {
625
        /* Preparations */
626
627
0
        switch (ok) {
628
0
        case 0:
629
0
            break;
630
0
        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
0
            if (data->count_output_structure == 0)
647
0
                return 0;
648
649
0
            original_data = data->ctx->construct(current_encoder_inst,
650
0
                data->ctx->construct_data);
651
652
            /* Also set the data type, using the encoder implementation name */
653
0
            data->data_type = OSSL_ENCODER_get0_name(current_encoder);
654
655
            /* Assume that the constructor recorded an error */
656
0
            if (original_data != NULL)
657
0
                ok = 1;
658
0
            else
659
0
                ok = 0;
660
0
            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
0
        }
691
692
        /* Calling the encoder implementation */
693
694
0
        if (ok) {
695
0
            OSSL_CORE_BIO *cbio = NULL;
696
0
            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
0
            if (top)
703
0
                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
0
            if (ok)
709
0
                ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
710
0
            if (ok) {
711
0
                ok = current_encoder->encode(current_encoder_ctx, cbio,
712
0
                    original_data, current_abstract,
713
0
                    data->ctx->selection,
714
0
                    ossl_pw_passphrase_callback_enc,
715
0
                    &data->ctx->pwdata);
716
0
                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
0
                OSSL_TRACE_END(ENCODER);
724
0
            }
725
726
0
            ossl_core_bio_free(cbio);
727
0
            data->prev_encoder_inst = current_encoder_inst;
728
0
        }
729
0
    }
730
731
    /* Cleanup and collecting the result */
732
733
0
    OPENSSL_free(data->running_output);
734
0
    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
0
    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
0
    BIO_free(allocated_out);
750
0
    if (original_data != NULL)
751
0
        data->ctx->cleanup(data->ctx->construct_data);
752
0
    return ok;
753
0
}
754
755
int ossl_bio_print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)
756
0
{
757
0
    int ret = 0, use_sep = 0;
758
0
    char *hex_str = NULL, *p;
759
0
    const char spaces[] = "    ";
760
0
    const char *post_label_spc = " ";
761
762
0
    const char *neg = "";
763
0
    int bytes;
764
765
0
    if (bn == NULL)
766
0
        return 0;
767
0
    if (label == NULL) {
768
0
        label = "";
769
0
        post_label_spc = "";
770
0
    }
771
772
0
    if (BN_is_zero(bn))
773
0
        return BIO_printf(out, "%s%s0\n", label, post_label_spc);
774
775
0
    if (BN_num_bytes(bn) <= BN_BYTES) {
776
0
        BN_ULONG *words = bn_get_words(bn);
777
778
0
        if (BN_is_negative(bn))
779
0
            neg = "-";
780
781
0
        return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
782
0
            label, post_label_spc, neg, words[0], neg, words[0]);
783
0
    }
784
785
0
    hex_str = BN_bn2hex(bn);
786
0
    if (hex_str == NULL)
787
0
        return 0;
788
789
0
    p = hex_str;
790
0
    if (*p == '-') {
791
0
        ++p;
792
0
        neg = " (Negative)";
793
0
    }
794
0
    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
0
    bytes = 0;
799
800
0
    if (BIO_printf(out, "%s", spaces) <= 0)
801
0
        goto err;
802
803
0
    while (*p != '\0') {
804
        /* Do a newline after every n hex bytes + add the space indent */
805
0
        if ((bytes % LABELED_BN_PRINT_WIDTH) == 0 && bytes > 0) {
806
0
            if (BIO_printf(out, ":\n%s", spaces) <= 0)
807
0
                goto err;
808
0
            use_sep = 0; /* The first byte on the next line doesn't have a : */
809
0
        }
810
0
        if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
811
0
                tolower((unsigned char)p[0]),
812
0
                tolower((unsigned char)p[1]))
813
0
            <= 0)
814
0
            goto err;
815
0
        ++bytes;
816
0
        p += 2;
817
0
        use_sep = 1;
818
0
    }
819
0
    if (BIO_printf(out, "\n") <= 0)
820
0
        goto err;
821
0
    ret = 1;
822
0
err:
823
0
    OPENSSL_free(hex_str);
824
0
    return ret;
825
0
}
826
827
int ossl_bio_print_labeled_buf(BIO *out, const char *label,
828
    const unsigned char *buf, size_t buflen)
829
0
{
830
0
    size_t i;
831
832
0
    if (BIO_printf(out, "%s\n", label) <= 0)
833
0
        return 0;
834
835
0
    for (i = 0; i < buflen; i++) {
836
0
        if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
837
0
            if (i > 0 && BIO_printf(out, "\n") <= 0)
838
0
                return 0;
839
0
            if (BIO_printf(out, "    ") <= 0)
840
0
                return 0;
841
0
        }
842
843
0
        if (BIO_printf(out, "%02x%s", buf[i],
844
0
                (i == buflen - 1) ? "" : ":")
845
0
            <= 0)
846
0
            return 0;
847
0
    }
848
0
    if (BIO_printf(out, "\n") <= 0)
849
0
        return 0;
850
851
0
    return 1;
852
0
}
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
0
{
857
0
    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
0
    if (!ossl_bio_print_labeled_bignum(out, "P:   ", ffc->p))
874
0
        goto err;
875
0
    if (ffc->q != NULL) {
876
0
        if (!ossl_bio_print_labeled_bignum(out, "Q:   ", ffc->q))
877
0
            goto err;
878
0
    }
879
0
    if (!ossl_bio_print_labeled_bignum(out, "G:   ", ffc->g))
880
0
        goto err;
881
0
    if (ffc->j != NULL) {
882
0
        if (!ossl_bio_print_labeled_bignum(out, "J:   ", ffc->j))
883
0
            goto err;
884
0
    }
885
0
    if (ffc->seed != NULL) {
886
0
        if (!ossl_bio_print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
887
0
            goto err;
888
0
    }
889
0
    if (ffc->gindex != -1) {
890
0
        if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
891
0
            goto err;
892
0
    }
893
0
    if (ffc->pcounter != -1) {
894
0
        if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
895
0
            goto err;
896
0
    }
897
0
    if (ffc->h != 0) {
898
0
        if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
899
0
            goto err;
900
0
    }
901
0
    return 1;
902
0
err:
903
0
    return 0;
904
0
}
905
906
#endif