Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/asn1/bio_asn1.c
Line
Count
Source (jump to first uncovered line)
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
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
105
0
        return 0;
106
0
    }
107
0
    if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
108
0
        OPENSSL_free(ctx);
109
0
        return 0;
110
0
    }
111
0
    BIO_set_data(b, ctx);
112
0
    BIO_set_init(b, 1);
113
114
0
    return 1;
115
0
}
116
117
static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
118
0
{
119
0
    if (size <= 0 || (ctx->buf = OPENSSL_malloc(size)) == NULL) {
120
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
121
0
        return 0;
122
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
242
0
        }
243
244
0
    }
245
246
0
 done:
247
0
    BIO_clear_retry_flags(b);
248
0
    BIO_copy_next_retry(b);
249
250
0
    return (wrlen > 0) ? wrlen : ret;
251
252
0
}
253
254
static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
255
                             asn1_ps_func *cleanup, asn1_bio_state_t next)
256
0
{
257
0
    int ret;
258
259
0
    if (ctx->ex_len <= 0)
260
0
        return 1;
261
0
    for (;;) {
262
0
        ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
263
0
        if (ret <= 0)
264
0
            break;
265
0
        ctx->ex_len -= ret;
266
0
        if (ctx->ex_len > 0)
267
0
            ctx->ex_pos += ret;
268
0
        else {
269
0
            if (cleanup)
270
0
                cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
271
0
            ctx->state = next;
272
0
            ctx->ex_pos = 0;
273
0
            break;
274
0
        }
275
0
    }
276
0
    return ret;
277
0
}
278
279
static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
280
                             asn1_ps_func *setup,
281
                             asn1_bio_state_t ex_state,
282
                             asn1_bio_state_t other_state)
283
0
{
284
0
    if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
285
0
        BIO_clear_retry_flags(b);
286
0
        return 0;
287
0
    }
288
0
    if (ctx->ex_len > 0)
289
0
        ctx->state = ex_state;
290
0
    else
291
0
        ctx->state = other_state;
292
0
    return 1;
293
0
}
294
295
static int asn1_bio_read(BIO *b, char *in, int inl)
296
0
{
297
0
    BIO *next = BIO_next(b);
298
0
    if (next == NULL)
299
0
        return 0;
300
0
    return BIO_read(next, in, inl);
301
0
}
302
303
static int asn1_bio_puts(BIO *b, const char *str)
304
0
{
305
0
    return asn1_bio_write(b, str, strlen(str));
306
0
}
307
308
static int asn1_bio_gets(BIO *b, char *str, int size)
309
0
{
310
0
    BIO *next = BIO_next(b);
311
0
    if (next == NULL)
312
0
        return 0;
313
0
    return BIO_gets(next, str, size);
314
0
}
315
316
static long asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
317
0
{
318
0
    BIO *next = BIO_next(b);
319
0
    if (next == NULL)
320
0
        return 0;
321
0
    return BIO_callback_ctrl(next, cmd, fp);
322
0
}
323
324
static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
325
0
{
326
0
    BIO_ASN1_BUF_CTX *ctx;
327
0
    BIO_ASN1_EX_FUNCS *ex_func;
328
0
    long ret = 1;
329
0
    BIO *next;
330
331
0
    ctx = BIO_get_data(b);
332
0
    if (ctx == NULL)
333
0
        return 0;
334
0
    next = BIO_next(b);
335
0
    switch (cmd) {
336
337
0
    case BIO_C_SET_PREFIX:
338
0
        ex_func = arg2;
339
0
        ctx->prefix = ex_func->ex_func;
340
0
        ctx->prefix_free = ex_func->ex_free_func;
341
0
        break;
342
343
0
    case BIO_C_GET_PREFIX:
344
0
        ex_func = arg2;
345
0
        ex_func->ex_func = ctx->prefix;
346
0
        ex_func->ex_free_func = ctx->prefix_free;
347
0
        break;
348
349
0
    case BIO_C_SET_SUFFIX:
350
0
        ex_func = arg2;
351
0
        ctx->suffix = ex_func->ex_func;
352
0
        ctx->suffix_free = ex_func->ex_free_func;
353
0
        break;
354
355
0
    case BIO_C_GET_SUFFIX:
356
0
        ex_func = arg2;
357
0
        ex_func->ex_func = ctx->suffix;
358
0
        ex_func->ex_free_func = ctx->suffix_free;
359
0
        break;
360
361
0
    case BIO_C_SET_EX_ARG:
362
0
        ctx->ex_arg = arg2;
363
0
        break;
364
365
0
    case BIO_C_GET_EX_ARG:
366
0
        *(void **)arg2 = ctx->ex_arg;
367
0
        break;
368
369
0
    case BIO_CTRL_FLUSH:
370
0
        if (next == NULL)
371
0
            return 0;
372
373
        /* Call post function if possible */
374
0
        if (ctx->state == ASN1_STATE_HEADER) {
375
0
            if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
376
0
                                   ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
377
0
                return 0;
378
0
        }
379
380
0
        if (ctx->state == ASN1_STATE_POST_COPY) {
381
0
            ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
382
0
                                    ASN1_STATE_DONE);
383
0
            if (ret <= 0)
384
0
                return ret;
385
0
        }
386
387
0
        if (ctx->state == ASN1_STATE_DONE)
388
0
            return BIO_ctrl(next, cmd, arg1, arg2);
389
0
        else {
390
0
            BIO_clear_retry_flags(b);
391
0
            return 0;
392
0
        }
393
394
0
    default:
395
0
        if (next == NULL)
396
0
            return 0;
397
0
        return BIO_ctrl(next, cmd, arg1, arg2);
398
399
0
    }
400
401
0
    return ret;
402
0
}
403
404
static int asn1_bio_set_ex(BIO *b, int cmd,
405
                           asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
406
0
{
407
0
    BIO_ASN1_EX_FUNCS extmp;
408
0
    extmp.ex_func = ex_func;
409
0
    extmp.ex_free_func = ex_free_func;
410
0
    return BIO_ctrl(b, cmd, 0, &extmp);
411
0
}
412
413
static int asn1_bio_get_ex(BIO *b, int cmd,
414
                           asn1_ps_func **ex_func,
415
                           asn1_ps_func **ex_free_func)
416
0
{
417
0
    BIO_ASN1_EX_FUNCS extmp;
418
0
    int ret;
419
0
    ret = BIO_ctrl(b, cmd, 0, &extmp);
420
0
    if (ret > 0) {
421
0
        *ex_func = extmp.ex_func;
422
0
        *ex_free_func = extmp.ex_free_func;
423
0
    }
424
0
    return ret;
425
0
}
426
427
int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
428
                        asn1_ps_func *prefix_free)
429
0
{
430
0
    return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
431
0
}
432
433
int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
434
                        asn1_ps_func **pprefix_free)
435
0
{
436
0
    return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
437
0
}
438
439
int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
440
                        asn1_ps_func *suffix_free)
441
0
{
442
0
    return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
443
0
}
444
445
int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
446
                        asn1_ps_func **psuffix_free)
447
0
{
448
0
    return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
449
0
}