Coverage Report

Created: 2025-12-31 06:58

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