Coverage Report

Created: 2026-04-01 06:39

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