Coverage Report

Created: 2025-12-08 06:22

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