Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/asn1/a_d2i_fp.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 <stdio.h>
11
#include <limits.h>
12
#include "internal/cryptlib.h"
13
#include "internal/numbers.h"
14
#include <openssl/buffer.h>
15
#include <openssl/asn1.h>
16
#include "internal/asn1.h"
17
#include "crypto/asn1.h"
18
19
#ifndef NO_OLD_ASN1
20
#ifndef OPENSSL_NO_STDIO
21
22
void *ASN1_d2i_fp(void *(*xnew)(void), d2i_of_void *d2i, FILE *in, void **x)
23
0
{
24
0
    BIO *b;
25
0
    void *ret;
26
27
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
28
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
29
0
        return NULL;
30
0
    }
31
0
    BIO_set_fp(b, in, BIO_NOCLOSE);
32
0
    ret = ASN1_d2i_bio(xnew, d2i, b, x);
33
0
    BIO_free(b);
34
0
    return ret;
35
0
}
36
#endif
37
38
void *ASN1_d2i_bio(void *(*xnew)(void), d2i_of_void *d2i, BIO *in, void **x)
39
0
{
40
0
    BUF_MEM *b = NULL;
41
0
    const unsigned char *p;
42
0
    void *ret = NULL;
43
0
    int len;
44
45
0
    len = asn1_d2i_read_bio(in, &b);
46
0
    if (len < 0)
47
0
        goto err;
48
49
0
    p = (unsigned char *)b->data;
50
0
    ret = d2i(x, &p, len);
51
0
err:
52
0
    BUF_MEM_free(b);
53
0
    return ret;
54
0
}
55
56
#endif
57
58
void *ASN1_item_d2i_bio_ex(const ASN1_ITEM *it, BIO *in, void *x,
59
    OSSL_LIB_CTX *libctx, const char *propq)
60
125k
{
61
125k
    BUF_MEM *b = NULL;
62
125k
    const unsigned char *p;
63
125k
    void *ret = NULL;
64
125k
    int len;
65
66
125k
    if (in == NULL)
67
0
        return NULL;
68
125k
    len = asn1_d2i_read_bio(in, &b);
69
125k
    if (len < 0)
70
11.3k
        goto err;
71
72
114k
    p = (const unsigned char *)b->data;
73
114k
    ret = ASN1_item_d2i_ex(x, &p, len, it, libctx, propq);
74
125k
err:
75
125k
    BUF_MEM_free(b);
76
125k
    return ret;
77
114k
}
78
79
void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
80
0
{
81
0
    return ASN1_item_d2i_bio_ex(it, in, x, NULL, NULL);
82
0
}
83
84
#ifndef OPENSSL_NO_STDIO
85
void *ASN1_item_d2i_fp_ex(const ASN1_ITEM *it, FILE *in, void *x,
86
    OSSL_LIB_CTX *libctx, const char *propq)
87
0
{
88
0
    BIO *b;
89
0
    char *ret;
90
91
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
92
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
93
0
        return NULL;
94
0
    }
95
0
    BIO_set_fp(b, in, BIO_NOCLOSE);
96
0
    ret = ASN1_item_d2i_bio_ex(it, b, x, libctx, propq);
97
0
    BIO_free(b);
98
0
    return ret;
99
0
}
100
101
void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
102
0
{
103
0
    return ASN1_item_d2i_fp_ex(it, in, x, NULL, NULL);
104
0
}
105
#endif
106
107
1.18G
#define HEADER_SIZE 2
108
343M
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
109
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
110
4.98M
{
111
4.98M
    BUF_MEM *b;
112
4.98M
    unsigned char *p;
113
4.98M
    int i;
114
4.98M
    size_t want = HEADER_SIZE;
115
4.98M
    uint32_t eos = 0;
116
4.98M
    size_t off = 0;
117
4.98M
    size_t len = 0;
118
4.98M
    size_t diff;
119
120
4.98M
    const unsigned char *q;
121
4.98M
    long slen;
122
4.98M
    int inf, tag, xclass;
123
124
4.98M
    b = BUF_MEM_new();
125
4.98M
    if (b == NULL) {
126
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
127
0
        return -1;
128
0
    }
129
130
4.98M
    ERR_set_mark();
131
1.21G
    for (;;) {
132
1.21G
        diff = len - off;
133
1.21G
        if (want >= diff) {
134
1.21G
            want -= diff;
135
136
1.21G
            if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
137
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
138
0
                goto err;
139
0
            }
140
1.21G
            i = BIO_read(in, &(b->data[len]), want);
141
1.21G
            if (i <= 0) {
142
212k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
143
212k
                goto err;
144
212k
            }
145
1.21G
            if (i > 0) {
146
1.21G
                if (len + i < len) {
147
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
148
0
                    goto err;
149
0
                }
150
1.21G
                len += i;
151
1.21G
                if ((size_t)i < want)
152
67.8k
                    continue;
153
1.21G
            }
154
1.21G
        }
155
        /* else data already loaded */
156
157
        /* make sure there is enough data for a complete header */
158
1.21G
        p = (unsigned char *)&(b->data[off]);
159
1.21G
        q = p;
160
1.21G
        diff = len - off;
161
1.21G
        if (diff < 2) {
162
            /* Failed sanity check */
163
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
164
0
            goto err;
165
0
        }
166
167
1.21G
        diff--;
168
1.21G
        if ((*(q++) & V_ASN1_PRIMITIVE_TAG) == V_ASN1_PRIMITIVE_TAG) {
169
28.7M
            unsigned int n = 0;
170
            /* Multi-byte tag.  See if we have the whole thing yet */
171
33.2M
            do {
172
33.2M
                if (n > 4) {
173
                    /* The tag value must fit into int */
174
17.5k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
175
17.5k
                    goto err;
176
17.5k
                }
177
33.2M
                ++n;
178
33.2M
                diff--;
179
33.2M
            } while (diff > 0 && *(q++) & 0x80);
180
181
28.7M
            if (diff == 0) {
182
                /*
183
                 * End of current data, will need at least 1 more byte for
184
                 * length.  2 if the tag is still incomplete
185
                 */
186
14.5M
                want = q - p + 2;
187
14.5M
                if (*q & 0x80) {
188
3.26M
                    want++;
189
3.26M
                }
190
14.5M
                continue;
191
14.5M
            }
192
28.7M
        }
193
194
        /* Check the length.  This should also work for indefinite length */
195
1.19G
        diff--;
196
1.19G
        if (*q & 0x80) {
197
363M
            unsigned int n = *q & 0x7f;
198
199
363M
            if (n > sizeof(long)) {
200
74.2k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
201
74.2k
                goto err;
202
74.2k
            }
203
363M
            if (n > diff) {
204
13.7M
                want = q - p + n + 1;
205
13.7M
                continue;
206
13.7M
            }
207
363M
        }
208
209
        /*
210
         * We have a complete header now, assuming we didn't hit EOF. Parse the
211
         * tag and length
212
         */
213
1.18G
        q = p;
214
1.18G
        diff = len - off;
215
1.18G
        inf = ASN1_get_object(&q, &slen, &tag, &xclass, (int)diff);
216
1.18G
        if (inf & 0x80) {
217
343M
            unsigned long e;
218
219
343M
            e = ERR_GET_REASON(ERR_peek_last_error());
220
343M
            if (e != ASN1_R_TOO_LONG)
221
22.1k
                goto err;
222
343M
            ERR_pop_to_mark();
223
343M
            ERR_set_mark();
224
343M
        }
225
1.18G
        off += q - p; /* end of data */
226
227
1.18G
        if (inf & 1) {
228
            /* no data body so go round again */
229
335M
            if (eos == UINT32_MAX) {
230
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
231
0
                goto err;
232
0
            }
233
335M
            eos++;
234
335M
            want = HEADER_SIZE;
235
849M
        } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
236
            /* eos value, so go back and read another header */
237
95.1M
            eos--;
238
95.1M
            if (eos == 0)
239
2.53M
                break;
240
92.6M
            else
241
92.6M
                want = HEADER_SIZE;
242
754M
        } else {
243
            /* suck in slen bytes of data */
244
754M
            want = slen;
245
754M
            if (want > (len - off)) {
246
343M
                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
247
248
343M
                want -= (len - off);
249
343M
                if (want > INT_MAX /* BIO_read takes an int length */ || len + want < len) {
250
43.6k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
251
43.6k
                    goto err;
252
43.6k
                }
253
686M
                while (want > 0) {
254
                    /*
255
                     * Read content in chunks of increasing size
256
                     * so we can return an error for EOF without
257
                     * having to allocate the entire content length
258
                     * in one go.
259
                     */
260
343M
                    size_t chunk = want > chunk_max ? chunk_max : want;
261
262
343M
                    if (!BUF_MEM_grow_clean(b, len + chunk)) {
263
0
                        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
264
0
                        goto err;
265
0
                    }
266
343M
                    want -= chunk;
267
687M
                    while (chunk > 0) {
268
343M
                        i = BIO_read(in, &(b->data[len]), chunk);
269
343M
                        if (i <= 0) {
270
206k
                            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
271
206k
                            goto err;
272
206k
                        }
273
                        /*
274
                         * This can't overflow because |len+want| didn't
275
                         * overflow.
276
                         */
277
343M
                        len += i;
278
343M
                        chunk -= i;
279
343M
                    }
280
343M
                    if (chunk_max < INT_MAX / 2)
281
343M
                        chunk_max *= 2;
282
343M
                }
283
343M
            }
284
754M
            if (off + slen < off) {
285
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
286
0
                goto err;
287
0
            }
288
754M
            off += slen;
289
754M
            if (eos == 0) {
290
1.87M
                break;
291
1.87M
            } else
292
752M
                want = HEADER_SIZE;
293
754M
        }
294
1.18G
    }
295
296
4.40M
    if (off > INT_MAX) {
297
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
298
0
        goto err;
299
0
    }
300
301
4.40M
    *pb = b;
302
4.40M
    ERR_clear_last_mark();
303
4.40M
    return off;
304
576k
err:
305
576k
    ERR_clear_last_mark();
306
576k
    BUF_MEM_free(b);
307
576k
    return -1;
308
4.40M
}