Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/asn1/bio_asn1.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2021 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
/*
11
 * Experimental ASN1 BIO. When written through the data is converted to an
12
 * ASN1 string type: default is OCTET STRING. Additional functions can be
13
 * provided to add prefix and suffix data.
14
 */
15
16
#include <string.h>
17
#include "internal/bio.h"
18
#include <openssl/asn1.h>
19
#include "internal/cryptlib.h"
20
21
/* Must be large enough for biggest tag+length */
22
0
#define DEFAULT_ASN1_BUF_SIZE 20
23
24
typedef enum {
25
    ASN1_STATE_START,
26
    ASN1_STATE_PRE_COPY,
27
    ASN1_STATE_HEADER,
28
    ASN1_STATE_HEADER_COPY,
29
    ASN1_STATE_DATA_COPY,
30
    ASN1_STATE_POST_COPY,
31
    ASN1_STATE_DONE
32
} asn1_bio_state_t;
33
34
typedef struct BIO_ASN1_EX_FUNCS_st {
35
    asn1_ps_func *ex_func;
36
    asn1_ps_func *ex_free_func;
37
} BIO_ASN1_EX_FUNCS;
38
39
typedef struct BIO_ASN1_BUF_CTX_t {
40
    /* Internal state */
41
    asn1_bio_state_t state;
42
    /* Internal buffer */
43
    unsigned char *buf;
44
    /* Size of buffer */
45
    int bufsize;
46
    /* Current position in buffer */
47
    int bufpos;
48
    /* Current buffer length */
49
    int buflen;
50
    /* Amount of data to copy */
51
    int copylen;
52
    /* Class and tag to use */
53
    int asn1_class, asn1_tag;
54
    asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
55
    /* Extra buffer for prefix and suffix data */
56
    unsigned char *ex_buf;
57
    int ex_len;
58
    int ex_pos;
59
    void *ex_arg;
60
} BIO_ASN1_BUF_CTX;
61
62
static int asn1_bio_write(BIO *h, const char *buf, int num);
63
static int asn1_bio_read(BIO *h, char *buf, int size);
64
static int asn1_bio_puts(BIO *h, const char *str);
65
static int asn1_bio_gets(BIO *h, char *str, int size);
66
static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
67
static int asn1_bio_new(BIO *h);
68
static int asn1_bio_free(BIO *data);
69
static long asn1_bio_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
70
71
static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
72
static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
73
    asn1_ps_func *cleanup, asn1_bio_state_t next);
74
static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
75
    asn1_ps_func *setup,
76
    asn1_bio_state_t ex_state,
77
    asn1_bio_state_t other_state);
78
79
static const BIO_METHOD methods_asn1 = {
80
    BIO_TYPE_ASN1,
81
    "asn1",
82
    bwrite_conv,
83
    asn1_bio_write,
84
    bread_conv,
85
    asn1_bio_read,
86
    asn1_bio_puts,
87
    asn1_bio_gets,
88
    asn1_bio_ctrl,
89
    asn1_bio_new,
90
    asn1_bio_free,
91
    asn1_bio_callback_ctrl,
92
};
93
94
const BIO_METHOD *BIO_f_asn1(void)
95
0
{
96
0
    return &methods_asn1;
97
0
}
98
99
static int asn1_bio_new(BIO *b)
100
0
{
101
0
    BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
102
103
0
    if (ctx == NULL)
104
0
        return 0;
105
0
    if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
106
0
        OPENSSL_free(ctx);
107
0
        return 0;
108
0
    }
109
0
    BIO_set_data(b, ctx);
110
0
    BIO_set_init(b, 1);
111
112
0
    return 1;
113
0
}
114
115
static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
116
0
{
117
0
    if (size <= 0) {
118
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
119
0
        return 0;
120
0
    }
121
0
    if ((ctx->buf = OPENSSL_malloc(size)) == NULL)
122
0
        return 0;
123
0
    ctx->bufsize = size;
124
0
    ctx->asn1_class = V_ASN1_UNIVERSAL;
125
0
    ctx->asn1_tag = V_ASN1_OCTET_STRING;
126
0
    ctx->state = ASN1_STATE_START;
127
0
    return 1;
128
0
}
129
130
static int asn1_bio_free(BIO *b)
131
0
{
132
0
    BIO_ASN1_BUF_CTX *ctx;
133
134
0
    if (b == NULL)
135
0
        return 0;
136
137
0
    ctx = BIO_get_data(b);
138
0
    if (ctx == NULL)
139
0
        return 0;
140
141
0
    if (ctx->prefix_free != NULL)
142
0
        ctx->prefix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
143
0
    if (ctx->suffix_free != NULL)
144
0
        ctx->suffix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
145
146
0
    OPENSSL_free(ctx->buf);
147
0
    OPENSSL_free(ctx);
148
0
    BIO_set_data(b, NULL);
149
0
    BIO_set_init(b, 0);
150
151
0
    return 1;
152
0
}
153
154
static int asn1_bio_write(BIO *b, const char *in, int inl)
155
0
{
156
0
    BIO_ASN1_BUF_CTX *ctx;
157
0
    int wrmax, wrlen, ret;
158
0
    unsigned char *p;
159
0
    BIO *next;
160
161
0
    ctx = BIO_get_data(b);
162
0
    next = BIO_next(b);
163
0
    if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
164
0
        return 0;
165
166
0
    wrlen = 0;
167
0
    ret = -1;
168
169
0
    for (;;) {
170
0
        switch (ctx->state) {
171
            /* Setup prefix data, call it */
172
0
        case ASN1_STATE_START:
173
0
            if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
174
0
                    ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
175
0
                return -1;
176
0
            break;
177
178
            /* Copy any pre data first */
179
0
        case ASN1_STATE_PRE_COPY:
180
181
0
            ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
182
0
                ASN1_STATE_HEADER);
183
184
0
            if (ret <= 0)
185
0
                goto done;
186
187
0
            break;
188
189
0
        case ASN1_STATE_HEADER:
190
0
            ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
191
0
            if (!ossl_assert(ctx->buflen <= ctx->bufsize))
192
0
                return -1;
193
0
            p = ctx->buf;
194
0
            ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
195
0
            ctx->copylen = inl;
196
0
            ctx->state = ASN1_STATE_HEADER_COPY;
197
198
0
            break;
199
200
0
        case ASN1_STATE_HEADER_COPY:
201
0
            ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
202
0
            if (ret <= 0)
203
0
                goto done;
204
205
0
            ctx->buflen -= ret;
206
0
            if (ctx->buflen)
207
0
                ctx->bufpos += ret;
208
0
            else {
209
0
                ctx->bufpos = 0;
210
0
                ctx->state = ASN1_STATE_DATA_COPY;
211
0
            }
212
213
0
            break;
214
215
0
        case ASN1_STATE_DATA_COPY:
216
217
0
            if (inl > ctx->copylen)
218
0
                wrmax = ctx->copylen;
219
0
            else
220
0
                wrmax = inl;
221
0
            ret = BIO_write(next, in, wrmax);
222
0
            if (ret <= 0)
223
0
                goto done;
224
0
            wrlen += ret;
225
0
            ctx->copylen -= ret;
226
0
            in += ret;
227
0
            inl -= ret;
228
229
0
            if (ctx->copylen == 0)
230
0
                ctx->state = ASN1_STATE_HEADER;
231
232
0
            if (inl == 0)
233
0
                goto done;
234
235
0
            break;
236
237
0
        case ASN1_STATE_POST_COPY:
238
0
        case ASN1_STATE_DONE:
239
0
            BIO_clear_retry_flags(b);
240
0
            return 0;
241
0
        }
242
0
    }
243
244
0
done:
245
0
    BIO_clear_retry_flags(b);
246
0
    BIO_copy_next_retry(b);
247
248
0
    return (wrlen > 0) ? wrlen : ret;
249
0
}
250
251
static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
252
    asn1_ps_func *cleanup, asn1_bio_state_t next)
253
0
{
254
0
    int ret;
255
256
0
    if (ctx->ex_len <= 0)
257
0
        return 1;
258
0
    for (;;) {
259
0
        ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
260
0
        if (ret <= 0)
261
0
            break;
262
0
        ctx->ex_len -= ret;
263
0
        if (ctx->ex_len > 0)
264
0
            ctx->ex_pos += ret;
265
0
        else {
266
0
            if (cleanup)
267
0
                cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
268
0
            ctx->state = next;
269
0
            ctx->ex_pos = 0;
270
0
            break;
271
0
        }
272
0
    }
273
0
    return ret;
274
0
}
275
276
static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
277
    asn1_ps_func *setup,
278
    asn1_bio_state_t ex_state,
279
    asn1_bio_state_t other_state)
280
0
{
281
0
    if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
282
0
        BIO_clear_retry_flags(b);
283
0
        return 0;
284
0
    }
285
0
    if (ctx->ex_len > 0)
286
0
        ctx->state = ex_state;
287
0
    else
288
0
        ctx->state = other_state;
289
0
    return 1;
290
0
}
291
292
static int asn1_bio_read(BIO *b, char *in, int inl)
293
0
{
294
0
    BIO *next = BIO_next(b);
295
0
    if (next == NULL)
296
0
        return 0;
297
0
    return BIO_read(next, in, inl);
298
0
}
299
300
static int asn1_bio_puts(BIO *b, const char *str)
301
0
{
302
0
    return asn1_bio_write(b, str, strlen(str));
303
0
}
304
305
static int asn1_bio_gets(BIO *b, char *str, int size)
306
0
{
307
0
    BIO *next = BIO_next(b);
308
0
    if (next == NULL)
309
0
        return 0;
310
0
    return BIO_gets(next, str, size);
311
0
}
312
313
static long asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
314
0
{
315
0
    BIO *next = BIO_next(b);
316
0
    if (next == NULL)
317
0
        return 0;
318
0
    return BIO_callback_ctrl(next, cmd, fp);
319
0
}
320
321
static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
322
0
{
323
0
    BIO_ASN1_BUF_CTX *ctx;
324
0
    BIO_ASN1_EX_FUNCS *ex_func;
325
0
    long ret = 1;
326
0
    BIO *next;
327
328
0
    ctx = BIO_get_data(b);
329
0
    if (ctx == NULL)
330
0
        return 0;
331
0
    next = BIO_next(b);
332
0
    switch (cmd) {
333
334
0
    case BIO_C_SET_PREFIX:
335
0
        ex_func = arg2;
336
0
        ctx->prefix = ex_func->ex_func;
337
0
        ctx->prefix_free = ex_func->ex_free_func;
338
0
        break;
339
340
0
    case BIO_C_GET_PREFIX:
341
0
        ex_func = arg2;
342
0
        ex_func->ex_func = ctx->prefix;
343
0
        ex_func->ex_free_func = ctx->prefix_free;
344
0
        break;
345
346
0
    case BIO_C_SET_SUFFIX:
347
0
        ex_func = arg2;
348
0
        ctx->suffix = ex_func->ex_func;
349
0
        ctx->suffix_free = ex_func->ex_free_func;
350
0
        break;
351
352
0
    case BIO_C_GET_SUFFIX:
353
0
        ex_func = arg2;
354
0
        ex_func->ex_func = ctx->suffix;
355
0
        ex_func->ex_free_func = ctx->suffix_free;
356
0
        break;
357
358
0
    case BIO_C_SET_EX_ARG:
359
0
        ctx->ex_arg = arg2;
360
0
        break;
361
362
0
    case BIO_C_GET_EX_ARG:
363
0
        *(void **)arg2 = ctx->ex_arg;
364
0
        break;
365
366
0
    case BIO_CTRL_FLUSH:
367
0
        if (next == NULL)
368
0
            return 0;
369
370
        /* Call post function if possible */
371
0
        if (ctx->state == ASN1_STATE_HEADER) {
372
0
            if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
373
0
                    ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
374
0
                return 0;
375
0
        }
376
377
0
        if (ctx->state == ASN1_STATE_POST_COPY) {
378
0
            ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
379
0
                ASN1_STATE_DONE);
380
0
            if (ret <= 0)
381
0
                return ret;
382
0
        }
383
384
0
        if (ctx->state == ASN1_STATE_DONE)
385
0
            return BIO_ctrl(next, cmd, arg1, arg2);
386
0
        else {
387
0
            BIO_clear_retry_flags(b);
388
0
            return 0;
389
0
        }
390
391
0
    default:
392
0
        if (next == NULL)
393
0
            return 0;
394
0
        return BIO_ctrl(next, cmd, arg1, arg2);
395
0
    }
396
397
0
    return ret;
398
0
}
399
400
static int asn1_bio_set_ex(BIO *b, int cmd,
401
    asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
402
0
{
403
0
    BIO_ASN1_EX_FUNCS extmp;
404
0
    extmp.ex_func = ex_func;
405
0
    extmp.ex_free_func = ex_free_func;
406
0
    return BIO_ctrl(b, cmd, 0, &extmp);
407
0
}
408
409
static int asn1_bio_get_ex(BIO *b, int cmd,
410
    asn1_ps_func **ex_func,
411
    asn1_ps_func **ex_free_func)
412
0
{
413
0
    BIO_ASN1_EX_FUNCS extmp;
414
0
    int ret;
415
0
    ret = BIO_ctrl(b, cmd, 0, &extmp);
416
0
    if (ret > 0) {
417
0
        *ex_func = extmp.ex_func;
418
0
        *ex_free_func = extmp.ex_free_func;
419
0
    }
420
0
    return ret;
421
0
}
422
423
int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
424
    asn1_ps_func *prefix_free)
425
0
{
426
0
    return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
427
0
}
428
429
int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
430
    asn1_ps_func **pprefix_free)
431
0
{
432
0
    return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
433
0
}
434
435
int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
436
    asn1_ps_func *suffix_free)
437
0
{
438
0
    return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
439
0
}
440
441
int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
442
    asn1_ps_func **psuffix_free)
443
0
{
444
0
    return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
445
0
}