Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/encode_decode/encoder_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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.57M
#define LABELED_BUF_PRINT_WIDTH 16
28
15.3M
#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
65.6k
{
71
65.6k
    struct encoder_process_data_st data;
72
73
65.6k
    memset(&data, 0, sizeof(data));
74
65.6k
    data.ctx = ctx;
75
65.6k
    data.bio = out;
76
65.6k
    data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
77
78
65.6k
    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
65.6k
    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
65.6k
    return encoder_process(&data) > 0;
92
65.6k
}
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
7.59k
{
123
7.59k
    BIO *out;
124
7.59k
    BUF_MEM *buf = NULL;
125
7.59k
    int ret = 0;
126
127
7.59k
    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
7.59k
    out = BIO_new(BIO_s_mem());
133
134
7.59k
    if (out != NULL
135
7.59k
        && OSSL_ENCODER_to_bio(ctx, out)
136
5.60k
        && BIO_get_mem_ptr(out, &buf) > 0) {
137
5.60k
        ret = 1; /* Hope for the best. A too small buffer will clear this */
138
139
5.60k
        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
5.60k
        } else {
150
            /* The buffer with the right size is already allocated for us */
151
5.60k
            *pdata_len = (size_t)buf->length;
152
5.60k
        }
153
154
5.60k
        if (ret) {
155
5.60k
            if (pdata != NULL) {
156
5.60k
                if (*pdata != NULL) {
157
0
                    memcpy(*pdata, buf->data, buf->length);
158
0
                    *pdata += buf->length;
159
5.60k
                } else {
160
                    /* In this case, we steal the data from BIO_s_mem() */
161
5.60k
                    *pdata = (unsigned char *)buf->data;
162
5.60k
                    buf->data = NULL;
163
5.60k
                }
164
5.60k
            }
165
5.60k
        }
166
5.60k
    }
167
7.59k
    BIO_free(out);
168
7.59k
    return ret;
169
7.59k
}
170
171
int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
172
17.1k
{
173
17.1k
    if (ctx == NULL) {
174
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
175
0
        return 0;
176
0
    }
177
178
17.1k
    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
17.1k
    if (selection == 0) {
184
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
185
0
        return 0;
186
0
    }
187
188
17.1k
    ctx->selection = selection;
189
17.1k
    return 1;
190
17.1k
}
191
192
int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
193
    const char *output_type)
194
17.1k
{
195
17.1k
    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
17.1k
    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
17.1k
    ctx->output_type = output_type;
206
17.1k
    return 1;
207
17.1k
}
208
209
int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
210
    const char *output_structure)
211
2.64k
{
212
2.64k
    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
2.64k
    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
2.64k
    ctx->output_structure = output_structure;
223
2.64k
    return 1;
224
2.64k
}
225
226
static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
227
    void *encoderctx)
228
146k
{
229
146k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
230
146k
    const OSSL_PROVIDER *prov;
231
146k
    OSSL_LIB_CTX *libctx;
232
146k
    const OSSL_PROPERTY_LIST *props;
233
146k
    const OSSL_PROPERTY_DEFINITION *prop;
234
235
146k
    if (encoder == NULL) {
236
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
237
0
        return 0;
238
0
    }
239
240
146k
    if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL)
241
0
        return 0;
242
243
146k
    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
146k
    prov = OSSL_ENCODER_get0_provider(encoder);
249
146k
    libctx = ossl_provider_libctx(prov);
250
146k
    props = ossl_encoder_parsed_properties(encoder);
251
146k
    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
146k
    prop = ossl_property_find_property(props, libctx, "output");
260
146k
    encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);
261
146k
    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
146k
    prop = ossl_property_find_property(props, libctx, "structure");
272
146k
    if (prop != NULL)
273
112k
        encoder_inst->output_structure
274
112k
            = ossl_property_get_string_value(libctx, prop);
275
276
146k
    encoder_inst->encoder = encoder;
277
146k
    encoder_inst->encoderctx = encoderctx;
278
146k
    return encoder_inst;
279
0
err:
280
0
    ossl_encoder_instance_free(encoder_inst);
281
0
    return NULL;
282
146k
}
283
284
void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
285
513k
{
286
513k
    if (encoder_inst != NULL) {
287
513k
        if (encoder_inst->encoder != NULL)
288
513k
            encoder_inst->encoder->freectx(encoder_inst->encoderctx);
289
513k
        encoder_inst->encoderctx = NULL;
290
513k
        OSSL_ENCODER_free(encoder_inst->encoder);
291
513k
        encoder_inst->encoder = NULL;
292
513k
        OPENSSL_free(encoder_inst);
293
513k
    }
294
513k
}
295
296
static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
297
    OSSL_ENCODER_INSTANCE *ei)
298
513k
{
299
513k
    int ok;
300
301
513k
    if (ctx->encoder_insts == NULL
302
65.6k
        && (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
513k
    ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
308
513k
    if (ok) {
309
513k
        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
513k
        OSSL_TRACE_END(ENCODER);
319
513k
    }
320
513k
    return ok;
321
513k
}
322
323
int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
324
146k
{
325
146k
    OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
326
146k
    const OSSL_PROVIDER *prov = NULL;
327
146k
    void *encoderctx = NULL;
328
146k
    void *provctx = NULL;
329
330
146k
    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
146k
    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
146k
    prov = OSSL_ENCODER_get0_provider(encoder);
341
146k
    provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
342
343
146k
    if ((encoderctx = encoder->newctx(provctx)) == NULL
344
146k
        || (encoder_inst = ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
345
0
        goto err;
346
    /* Avoid double free of encoderctx on further errors */
347
146k
    encoderctx = NULL;
348
349
146k
    if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
350
0
        goto err;
351
352
146k
    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
146k
}
359
360
int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
361
    OSSL_LIB_CTX *libctx, const char *propq)
362
17.1k
{
363
17.1k
    if (ctx == NULL) {
364
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
365
0
        return 0;
366
0
    }
367
368
17.1k
    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
17.1k
    return 1;
374
17.1k
}
375
376
int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
377
255k
{
378
255k
    if (ctx == NULL || ctx->encoder_insts == NULL)
379
1.04k
        return 0;
380
254k
    return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
381
255k
}
382
383
int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
384
    OSSL_ENCODER_CONSTRUCT *construct)
385
17.0k
{
386
17.0k
    if (ctx == NULL) {
387
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
388
0
        return 0;
389
0
    }
390
391
17.0k
    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
17.0k
    ctx->construct = construct;
397
17.0k
    return 1;
398
17.0k
}
399
400
int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
401
    void *construct_data)
402
17.0k
{
403
17.0k
    if (ctx == NULL) {
404
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
405
0
        return 0;
406
0
    }
407
408
17.0k
    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
17.0k
    ctx->construct_data = construct_data;
414
17.0k
    return 1;
415
17.0k
}
416
417
int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
418
    OSSL_ENCODER_CLEANUP *cleanup)
419
17.0k
{
420
17.0k
    if (ctx == NULL) {
421
0
        ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
422
0
        return 0;
423
0
    }
424
425
17.0k
    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
17.0k
    ctx->cleanup = cleanup;
431
17.0k
    return 1;
432
17.0k
}
433
434
OSSL_ENCODER *
435
OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
436
1.10M
{
437
1.10M
    if (encoder_inst == NULL)
438
0
        return NULL;
439
1.10M
    return encoder_inst->encoder;
440
1.10M
}
441
442
void *
443
OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
444
1.02M
{
445
1.02M
    if (encoder_inst == NULL)
446
0
        return NULL;
447
1.02M
    return encoder_inst->encoderctx;
448
1.02M
}
449
450
const char *
451
OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
452
513k
{
453
513k
    if (encoder_inst == NULL)
454
0
        return NULL;
455
513k
    return encoder_inst->output_type;
456
513k
}
457
458
const char *
459
OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
460
513k
{
461
513k
    if (encoder_inst == NULL)
462
0
        return NULL;
463
513k
    return encoder_inst->output_structure;
464
513k
}
465
466
static int encoder_process(struct encoder_process_data_st *data)
467
131k
{
468
131k
    OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
469
131k
    OSSL_ENCODER *current_encoder = NULL;
470
131k
    OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
471
131k
    BIO *allocated_out = NULL;
472
131k
    const void *original_data = NULL;
473
131k
    OSSL_PARAM abstract[10];
474
131k
    const OSSL_PARAM *current_abstract = NULL;
475
131k
    int i;
476
131k
    int ok = -1; /* -1 signifies that the lookup loop gave nothing */
477
131k
    int top = 0;
478
479
131k
    if (data->next_encoder_inst == NULL) {
480
        /* First iteration, where we prepare for what is to come */
481
482
65.6k
        data->count_output_structure = data->ctx->output_structure == NULL ? -1 : 0;
483
65.6k
        top = 1;
484
65.6k
    }
485
486
578k
    for (i = data->current_encoder_inst_index; i-- > 0;) {
487
513k
        OSSL_ENCODER *next_encoder = NULL;
488
513k
        const char *current_output_type;
489
513k
        const char *current_output_structure;
490
513k
        struct encoder_process_data_st new_data;
491
492
513k
        if (!top)
493
11.2k
            next_encoder = OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
494
495
513k
        current_encoder_inst = sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
496
513k
        current_encoder = OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
497
513k
        current_encoder_ctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
498
513k
        current_output_type = OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
499
513k
        current_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
500
513k
        memset(&new_data, 0, sizeof(new_data));
501
513k
        new_data.ctx = data->ctx;
502
513k
        new_data.current_encoder_inst_index = i;
503
513k
        new_data.next_encoder_inst = current_encoder_inst;
504
513k
        new_data.count_output_structure = data->count_output_structure;
505
513k
        new_data.level = data->level + 1;
506
507
513k
        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
513k
        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
513k
        if (top) {
524
501k
            if (data->ctx->output_type != NULL
525
501k
                && OPENSSL_strcasecmp(current_output_type,
526
501k
                       data->ctx->output_type)
527
501k
                    != 0) {
528
411k
                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
411k
                OSSL_TRACE_END(ENCODER);
536
411k
                continue;
537
411k
            }
538
501k
        } else {
539
11.2k
            if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
540
11.2k
                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
11.2k
                OSSL_TRACE_END(ENCODER);
548
11.2k
                continue;
549
11.2k
            }
550
11.2k
        }
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
90.0k
        if (data->ctx->output_structure != NULL
558
32.0k
            && current_output_structure != NULL) {
559
32.0k
            if (OPENSSL_strcasecmp(data->ctx->output_structure,
560
32.0k
                    current_output_structure)
561
32.0k
                != 0) {
562
24.6k
                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
24.6k
                OSSL_TRACE_END(ENCODER);
571
24.6k
                continue;
572
24.6k
            }
573
574
7.33k
            data->count_output_structure++;
575
7.33k
        }
576
577
        /*
578
         * Recurse to process the encoder implementations before the current
579
         * one.
580
         */
581
65.3k
        ok = encoder_process(&new_data);
582
583
65.3k
        data->prev_encoder_inst = new_data.prev_encoder_inst;
584
65.3k
        data->running_output = new_data.running_output;
585
65.3k
        data->running_output_length = new_data.running_output_length;
586
65.3k
        data->data_type = new_data.data_type;
587
588
        /*
589
         * ok == -1     means that the recursion call above gave no further
590
         *              encoders, and that the one we're currently at should
591
         *              be tried.
592
         * ok == 0      means that something failed in the recursion call
593
         *              above, making the result unsuitable for a chain.
594
         *              In this case, we simply continue to try finding a
595
         *              suitable encoder at this recursion level.
596
         * ok == 1      means that the recursion call was successful, and we
597
         *              try to use the result at this recursion level.
598
         */
599
65.3k
        if (ok != 0)
600
65.3k
            break;
601
602
0
        OSSL_TRACE_BEGIN(ENCODER)
603
0
        {
604
0
            BIO_printf(trc_out,
605
0
                "[%d]    Skipping because recursion level %d failed\n",
606
0
                data->level, new_data.level);
607
0
        }
608
0
        OSSL_TRACE_END(ENCODER);
609
0
    }
610
611
    /*
612
     * If |i < 0|, we didn't find any useful encoder in this recursion, so
613
     * we do the rest of the process only if |i >= 0|.
614
     */
615
131k
    if (i < 0) {
616
65.6k
        ok = -1;
617
618
65.6k
        OSSL_TRACE_BEGIN(ENCODER)
619
0
        {
620
0
            BIO_printf(trc_out,
621
0
                "[%d] (ctx %p) No suitable encoder found\n",
622
0
                data->level, (void *)data->ctx);
623
0
        }
624
65.6k
        OSSL_TRACE_END(ENCODER);
625
65.6k
    } else {
626
        /* Preparations */
627
628
65.3k
        switch (ok) {
629
0
        case 0:
630
0
            break;
631
65.3k
        case -1:
632
            /*
633
             * We have reached the beginning of the encoder instance sequence,
634
             * so we prepare the object to be encoded.
635
             */
636
637
            /*
638
             * |data->count_output_structure| is one of these values:
639
             *
640
             * -1       There is no desired output structure
641
             *  0       There is a desired output structure, and it wasn't
642
             *          matched by any of the encoder instances that were
643
             *          considered
644
             * >0       There is a desired output structure, and at least one
645
             *          of the encoder instances matched it
646
             */
647
65.3k
            if (data->count_output_structure == 0)
648
0
                return 0;
649
650
65.3k
            original_data = data->ctx->construct(current_encoder_inst,
651
65.3k
                data->ctx->construct_data);
652
653
            /* Also set the data type, using the encoder implementation name */
654
65.3k
            data->data_type = OSSL_ENCODER_get0_name(current_encoder);
655
656
            /* Assume that the constructor recorded an error */
657
65.3k
            if (original_data != NULL)
658
65.3k
                ok = 1;
659
0
            else
660
0
                ok = 0;
661
65.3k
            break;
662
0
        case 1:
663
0
            if (!ossl_assert(data->running_output != NULL)) {
664
0
                ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
665
0
                ok = 0;
666
0
                break;
667
0
            }
668
669
0
            {
670
                /*
671
                 * Create an object abstraction from the latest output, which
672
                 * was stolen from the previous round.
673
                 */
674
675
0
                OSSL_PARAM *abstract_p = abstract;
676
0
                const char *prev_output_structure = OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
677
678
0
                *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
679
0
                    (char *)data->data_type, 0);
680
0
                if (prev_output_structure != NULL)
681
0
                    *abstract_p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
682
0
                        (char *)prev_output_structure,
683
0
                        0);
684
0
                *abstract_p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
685
0
                    data->running_output,
686
0
                    data->running_output_length);
687
0
                *abstract_p = OSSL_PARAM_construct_end();
688
0
                current_abstract = abstract;
689
0
            }
690
0
            break;
691
65.3k
        }
692
693
        /* Calling the encoder implementation */
694
695
65.3k
        if (ok) {
696
65.3k
            OSSL_CORE_BIO *cbio = NULL;
697
65.3k
            BIO *current_out = NULL;
698
699
            /*
700
             * If we're at the last encoder instance to use, we're setting up
701
             * final output.  Otherwise, set up an intermediary memory output.
702
             */
703
65.3k
            if (top)
704
65.3k
                current_out = data->bio;
705
0
            else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
706
0
                == NULL)
707
0
                ok = 0; /* Assume BIO_new() recorded an error */
708
709
65.3k
            if (ok)
710
65.3k
                ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
711
65.3k
            if (ok) {
712
65.3k
                ok = current_encoder->encode(current_encoder_ctx, cbio,
713
65.3k
                    original_data, current_abstract,
714
65.3k
                    data->ctx->selection,
715
65.3k
                    ossl_pw_passphrase_callback_enc,
716
65.3k
                    &data->ctx->pwdata);
717
65.3k
                OSSL_TRACE_BEGIN(ENCODER)
718
0
                {
719
0
                    BIO_printf(trc_out,
720
0
                        "[%d] (ctx %p) Running encoder instance %p => %d\n",
721
0
                        data->level, (void *)data->ctx,
722
0
                        (void *)current_encoder_inst, ok);
723
0
                }
724
65.3k
                OSSL_TRACE_END(ENCODER);
725
65.3k
            }
726
727
65.3k
            ossl_core_bio_free(cbio);
728
65.3k
            data->prev_encoder_inst = current_encoder_inst;
729
65.3k
        }
730
65.3k
    }
731
732
    /* Cleanup and collecting the result */
733
734
131k
    OPENSSL_free(data->running_output);
735
131k
    data->running_output = NULL;
736
737
    /*
738
     * Steal the output from the BIO_s_mem, if we did allocate one.
739
     * That'll be the data for an object abstraction in the next round.
740
     */
741
131k
    if (allocated_out != NULL) {
742
0
        BUF_MEM *buf;
743
744
0
        BIO_get_mem_ptr(allocated_out, &buf);
745
0
        data->running_output = (unsigned char *)buf->data;
746
0
        data->running_output_length = buf->length;
747
0
        memset(buf, 0, sizeof(*buf));
748
0
    }
749
750
131k
    BIO_free(allocated_out);
751
131k
    if (original_data != NULL)
752
65.3k
        data->ctx->cleanup(data->ctx->construct_data);
753
131k
    return ok;
754
131k
}
755
756
int ossl_bio_print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)
757
29.7k
{
758
29.7k
    int ret = 0, use_sep = 0;
759
29.7k
    char *hex_str = NULL, *p;
760
29.7k
    const char spaces[] = "    ";
761
29.7k
    const char *post_label_spc = " ";
762
763
29.7k
    const char *neg = "";
764
29.7k
    int bytes;
765
766
29.7k
    if (bn == NULL)
767
374
        return 0;
768
29.3k
    if (label == NULL) {
769
1.84k
        label = "";
770
1.84k
        post_label_spc = "";
771
1.84k
    }
772
773
29.3k
    if (BN_is_zero(bn))
774
1.26k
        return BIO_printf(out, "%s%s0\n", label, post_label_spc);
775
776
28.1k
    if (BN_num_bytes(bn) <= BN_BYTES) {
777
12.8k
        BN_ULONG *words = bn_get_words(bn);
778
779
12.8k
        if (BN_is_negative(bn))
780
44
            neg = "-";
781
782
12.8k
        return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
783
12.8k
            label, post_label_spc, neg, words[0], neg, words[0]);
784
12.8k
    }
785
786
15.2k
    hex_str = BN_bn2hex(bn);
787
15.2k
    if (hex_str == NULL)
788
0
        return 0;
789
790
15.2k
    p = hex_str;
791
15.2k
    if (*p == '-') {
792
79
        ++p;
793
79
        neg = " (Negative)";
794
79
    }
795
15.2k
    if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
796
0
        goto err;
797
798
    /* Keep track of how many bytes we have printed out so far */
799
15.2k
    bytes = 0;
800
801
15.2k
    if (BIO_printf(out, "%s", spaces) <= 0)
802
0
        goto err;
803
804
15.3M
    while (*p != '\0') {
805
        /* Do a newline after every n hex bytes + add the space indent */
806
15.3M
        if ((bytes % LABELED_BN_PRINT_WIDTH) == 0 && bytes > 0) {
807
947k
            if (BIO_printf(out, ":\n%s", spaces) <= 0)
808
0
                goto err;
809
947k
            use_sep = 0; /* The first byte on the next line doesn't have a : */
810
947k
        }
811
15.3M
        if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
812
15.3M
                tolower((unsigned char)p[0]),
813
15.3M
                tolower((unsigned char)p[1]))
814
15.3M
            <= 0)
815
0
            goto err;
816
15.3M
        ++bytes;
817
15.3M
        p += 2;
818
15.3M
        use_sep = 1;
819
15.3M
    }
820
15.2k
    if (BIO_printf(out, "\n") <= 0)
821
0
        goto err;
822
15.2k
    ret = 1;
823
15.2k
err:
824
15.2k
    OPENSSL_free(hex_str);
825
15.2k
    return ret;
826
15.2k
}
827
828
int ossl_bio_print_labeled_buf(BIO *out, const char *label,
829
    const unsigned char *buf, size_t buflen)
830
12.7k
{
831
12.7k
    size_t i;
832
833
12.7k
    if (BIO_printf(out, "%s\n", label) <= 0)
834
0
        return 0;
835
836
2.58M
    for (i = 0; i < buflen; i++) {
837
2.57M
        if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
838
172k
            if (i > 0 && BIO_printf(out, "\n") <= 0)
839
0
                return 0;
840
172k
            if (BIO_printf(out, "    ") <= 0)
841
0
                return 0;
842
172k
        }
843
844
2.57M
        if (BIO_printf(out, "%02x%s", buf[i],
845
2.57M
                (i == buflen - 1) ? "" : ":")
846
2.57M
            <= 0)
847
0
            return 0;
848
2.57M
    }
849
12.7k
    if (BIO_printf(out, "\n") <= 0)
850
0
        return 0;
851
852
12.7k
    return 1;
853
12.7k
}
854
855
#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
856
int ossl_bio_print_ffc_params(BIO *out, const FFC_PARAMS *ffc)
857
12.2k
{
858
12.2k
    if (ffc->nid != NID_undef) {
859
0
#ifndef OPENSSL_NO_DH
860
0
        const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);
861
0
        const char *name = ossl_ffc_named_group_get_name(group);
862
863
0
        if (name == NULL)
864
0
            goto err;
865
0
        if (BIO_printf(out, "GROUP: %s\n", name) <= 0)
866
0
            goto err;
867
0
        return 1;
868
#else
869
        /* How could this be? We should not have a nid in a no-dh build. */
870
        goto err;
871
#endif
872
0
    }
873
874
12.2k
    if (!ossl_bio_print_labeled_bignum(out, "P:   ", ffc->p))
875
0
        goto err;
876
12.2k
    if (ffc->q != NULL) {
877
10.2k
        if (!ossl_bio_print_labeled_bignum(out, "Q:   ", ffc->q))
878
0
            goto err;
879
10.2k
    }
880
12.2k
    if (!ossl_bio_print_labeled_bignum(out, "G:   ", ffc->g))
881
0
        goto err;
882
12.2k
    if (ffc->j != NULL) {
883
117
        if (!ossl_bio_print_labeled_bignum(out, "J:   ", ffc->j))
884
0
            goto err;
885
117
    }
886
12.2k
    if (ffc->seed != NULL) {
887
125
        if (!ossl_bio_print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))
888
0
            goto err;
889
125
    }
890
12.2k
    if (ffc->gindex != -1) {
891
0
        if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)
892
0
            goto err;
893
0
    }
894
12.2k
    if (ffc->pcounter != -1) {
895
153
        if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)
896
0
            goto err;
897
153
    }
898
12.2k
    if (ffc->h != 0) {
899
0
        if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)
900
0
            goto err;
901
0
    }
902
12.2k
    return 1;
903
0
err:
904
0
    return 0;
905
12.2k
}
906
907
#endif