Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/asn1/bio_asn1.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
    /* TODO: Convert to new style write function */
83
    bwrite_conv,
84
    asn1_bio_write,
85
    /* TODO: Convert to new style read function */
86
    bread_conv,
87
    asn1_bio_read,
88
    asn1_bio_puts,
89
    asn1_bio_gets,
90
    asn1_bio_ctrl,
91
    asn1_bio_new,
92
    asn1_bio_free,
93
    asn1_bio_callback_ctrl,
94
};
95
96
const BIO_METHOD *BIO_f_asn1(void)
97
0
{
98
0
    return &methods_asn1;
99
0
}
100
101
static int asn1_bio_new(BIO *b)
102
0
{
103
0
    BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
104
0
105
0
    if (ctx == NULL)
106
0
        return 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
0
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 ((ctx->buf = OPENSSL_malloc(size)) == NULL) {
120
0
        ASN1err(ASN1_F_ASN1_BIO_INIT, 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
0
134
0
    if (b == NULL)
135
0
        return 0;
136
0
137
0
    ctx = BIO_get_data(b);
138
0
    if (ctx == NULL)
139
0
        return 0;
140
0
141
0
    OPENSSL_free(ctx->buf);
142
0
    OPENSSL_free(ctx);
143
0
    BIO_set_data(b, NULL);
144
0
    BIO_set_init(b, 0);
145
0
146
0
    return 1;
147
0
}
148
149
static int asn1_bio_write(BIO *b, const char *in, int inl)
150
0
{
151
0
    BIO_ASN1_BUF_CTX *ctx;
152
0
    int wrmax, wrlen, ret;
153
0
    unsigned char *p;
154
0
    BIO *next;
155
0
156
0
    ctx = BIO_get_data(b);
157
0
    next = BIO_next(b);
158
0
    if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
159
0
        return 0;
160
0
161
0
    wrlen = 0;
162
0
    ret = -1;
163
0
164
0
    for (;;) {
165
0
        switch (ctx->state) {
166
0
            /* Setup prefix data, call it */
167
0
        case ASN1_STATE_START:
168
0
            if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
169
0
                                   ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
170
0
                return 0;
171
0
            break;
172
0
173
0
            /* Copy any pre data first */
174
0
        case ASN1_STATE_PRE_COPY:
175
0
176
0
            ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
177
0
                                    ASN1_STATE_HEADER);
178
0
179
0
            if (ret <= 0)
180
0
                goto done;
181
0
182
0
            break;
183
0
184
0
        case ASN1_STATE_HEADER:
185
0
            ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
186
0
            if (!ossl_assert(ctx->buflen <= ctx->bufsize))
187
0
                return 0;
188
0
            p = ctx->buf;
189
0
            ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
190
0
            ctx->copylen = inl;
191
0
            ctx->state = ASN1_STATE_HEADER_COPY;
192
0
193
0
            break;
194
0
195
0
        case ASN1_STATE_HEADER_COPY:
196
0
            ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
197
0
            if (ret <= 0)
198
0
                goto done;
199
0
200
0
            ctx->buflen -= ret;
201
0
            if (ctx->buflen)
202
0
                ctx->bufpos += ret;
203
0
            else {
204
0
                ctx->bufpos = 0;
205
0
                ctx->state = ASN1_STATE_DATA_COPY;
206
0
            }
207
0
208
0
            break;
209
0
210
0
        case ASN1_STATE_DATA_COPY:
211
0
212
0
            if (inl > ctx->copylen)
213
0
                wrmax = ctx->copylen;
214
0
            else
215
0
                wrmax = inl;
216
0
            ret = BIO_write(next, in, wrmax);
217
0
            if (ret <= 0)
218
0
                goto done;
219
0
            wrlen += ret;
220
0
            ctx->copylen -= ret;
221
0
            in += ret;
222
0
            inl -= ret;
223
0
224
0
            if (ctx->copylen == 0)
225
0
                ctx->state = ASN1_STATE_HEADER;
226
0
227
0
            if (inl == 0)
228
0
                goto done;
229
0
230
0
            break;
231
0
232
0
        case ASN1_STATE_POST_COPY:
233
0
        case ASN1_STATE_DONE:
234
0
            BIO_clear_retry_flags(b);
235
0
            return 0;
236
0
237
0
        }
238
0
239
0
    }
240
0
241
0
 done:
242
0
    BIO_clear_retry_flags(b);
243
0
    BIO_copy_next_retry(b);
244
0
245
0
    return (wrlen > 0) ? wrlen : ret;
246
0
247
0
}
248
249
static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
250
                             asn1_ps_func *cleanup, asn1_bio_state_t next)
251
0
{
252
0
    int ret;
253
0
254
0
    if (ctx->ex_len <= 0)
255
0
        return 1;
256
0
    for (;;) {
257
0
        ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
258
0
        if (ret <= 0)
259
0
            break;
260
0
        ctx->ex_len -= ret;
261
0
        if (ctx->ex_len > 0)
262
0
            ctx->ex_pos += ret;
263
0
        else {
264
0
            if (cleanup)
265
0
                cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
266
0
            ctx->state = next;
267
0
            ctx->ex_pos = 0;
268
0
            break;
269
0
        }
270
0
    }
271
0
    return ret;
272
0
}
273
274
static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
275
                             asn1_ps_func *setup,
276
                             asn1_bio_state_t ex_state,
277
                             asn1_bio_state_t other_state)
278
0
{
279
0
    if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
280
0
        BIO_clear_retry_flags(b);
281
0
        return 0;
282
0
    }
283
0
    if (ctx->ex_len > 0)
284
0
        ctx->state = ex_state;
285
0
    else
286
0
        ctx->state = other_state;
287
0
    return 1;
288
0
}
289
290
static int asn1_bio_read(BIO *b, char *in, int inl)
291
0
{
292
0
    BIO *next = BIO_next(b);
293
0
    if (next == NULL)
294
0
        return 0;
295
0
    return BIO_read(next, in, inl);
296
0
}
297
298
static int asn1_bio_puts(BIO *b, const char *str)
299
0
{
300
0
    return asn1_bio_write(b, str, strlen(str));
301
0
}
302
303
static int asn1_bio_gets(BIO *b, char *str, int size)
304
0
{
305
0
    BIO *next = BIO_next(b);
306
0
    if (next == NULL)
307
0
        return 0;
308
0
    return BIO_gets(next, str, size);
309
0
}
310
311
static long asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
312
0
{
313
0
    BIO *next = BIO_next(b);
314
0
    if (next == NULL)
315
0
        return 0;
316
0
    return BIO_callback_ctrl(next, cmd, fp);
317
0
}
318
319
static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
320
0
{
321
0
    BIO_ASN1_BUF_CTX *ctx;
322
0
    BIO_ASN1_EX_FUNCS *ex_func;
323
0
    long ret = 1;
324
0
    BIO *next;
325
0
326
0
    ctx = BIO_get_data(b);
327
0
    if (ctx == NULL)
328
0
        return 0;
329
0
    next = BIO_next(b);
330
0
    switch (cmd) {
331
0
332
0
    case BIO_C_SET_PREFIX:
333
0
        ex_func = arg2;
334
0
        ctx->prefix = ex_func->ex_func;
335
0
        ctx->prefix_free = ex_func->ex_free_func;
336
0
        break;
337
0
338
0
    case BIO_C_GET_PREFIX:
339
0
        ex_func = arg2;
340
0
        ex_func->ex_func = ctx->prefix;
341
0
        ex_func->ex_free_func = ctx->prefix_free;
342
0
        break;
343
0
344
0
    case BIO_C_SET_SUFFIX:
345
0
        ex_func = arg2;
346
0
        ctx->suffix = ex_func->ex_func;
347
0
        ctx->suffix_free = ex_func->ex_free_func;
348
0
        break;
349
0
350
0
    case BIO_C_GET_SUFFIX:
351
0
        ex_func = arg2;
352
0
        ex_func->ex_func = ctx->suffix;
353
0
        ex_func->ex_free_func = ctx->suffix_free;
354
0
        break;
355
0
356
0
    case BIO_C_SET_EX_ARG:
357
0
        ctx->ex_arg = arg2;
358
0
        break;
359
0
360
0
    case BIO_C_GET_EX_ARG:
361
0
        *(void **)arg2 = ctx->ex_arg;
362
0
        break;
363
0
364
0
    case BIO_CTRL_FLUSH:
365
0
        if (next == NULL)
366
0
            return 0;
367
0
368
0
        /* Call post function if possible */
369
0
        if (ctx->state == ASN1_STATE_HEADER) {
370
0
            if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
371
0
                                   ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
372
0
                return 0;
373
0
        }
374
0
375
0
        if (ctx->state == ASN1_STATE_POST_COPY) {
376
0
            ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
377
0
                                    ASN1_STATE_DONE);
378
0
            if (ret <= 0)
379
0
                return ret;
380
0
        }
381
0
382
0
        if (ctx->state == ASN1_STATE_DONE)
383
0
            return BIO_ctrl(next, cmd, arg1, arg2);
384
0
        else {
385
0
            BIO_clear_retry_flags(b);
386
0
            return 0;
387
0
        }
388
0
389
0
    default:
390
0
        if (next == NULL)
391
0
            return 0;
392
0
        return BIO_ctrl(next, cmd, arg1, arg2);
393
0
394
0
    }
395
0
396
0
    return ret;
397
0
}
398
399
static int asn1_bio_set_ex(BIO *b, int cmd,
400
                           asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
401
0
{
402
0
    BIO_ASN1_EX_FUNCS extmp;
403
0
    extmp.ex_func = ex_func;
404
0
    extmp.ex_free_func = ex_free_func;
405
0
    return BIO_ctrl(b, cmd, 0, &extmp);
406
0
}
407
408
static int asn1_bio_get_ex(BIO *b, int cmd,
409
                           asn1_ps_func **ex_func,
410
                           asn1_ps_func **ex_free_func)
411
0
{
412
0
    BIO_ASN1_EX_FUNCS extmp;
413
0
    int ret;
414
0
    ret = BIO_ctrl(b, cmd, 0, &extmp);
415
0
    if (ret > 0) {
416
0
        *ex_func = extmp.ex_func;
417
0
        *ex_free_func = extmp.ex_free_func;
418
0
    }
419
0
    return ret;
420
0
}
421
422
int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
423
                        asn1_ps_func *prefix_free)
424
0
{
425
0
    return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
426
0
}
427
428
int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
429
                        asn1_ps_func **pprefix_free)
430
0
{
431
0
    return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
432
0
}
433
434
int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
435
                        asn1_ps_func *suffix_free)
436
0
{
437
0
    return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
438
0
}
439
440
int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
441
                        asn1_ps_func **psuffix_free)
442
0
{
443
0
    return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
444
0
}