Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
126k
{
61
126k
    BUF_MEM *b = NULL;
62
126k
    const unsigned char *p;
63
126k
    void *ret = NULL;
64
126k
    int len;
65
66
126k
    if (in == NULL)
67
0
        return NULL;
68
126k
    len = asn1_d2i_read_bio(in, &b);
69
126k
    if (len < 0)
70
11.2k
        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
126k
err:
75
126k
    BUF_MEM_free(b);
76
126k
    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.16G
#define HEADER_SIZE 2
108
353M
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
109
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
110
5.02M
{
111
5.02M
    BUF_MEM *b;
112
5.02M
    unsigned char *p;
113
5.02M
    int i;
114
5.02M
    size_t want = HEADER_SIZE;
115
5.02M
    uint32_t eos = 0;
116
5.02M
    size_t off = 0;
117
5.02M
    size_t len = 0;
118
5.02M
    size_t diff;
119
120
5.02M
    const unsigned char *q;
121
5.02M
    long slen;
122
5.02M
    int inf, tag, xclass;
123
124
5.02M
    b = BUF_MEM_new();
125
5.02M
    if (b == NULL) {
126
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
127
0
        return -1;
128
0
    }
129
130
5.02M
    ERR_set_mark();
131
1.18G
    for (;;) {
132
1.18G
        diff = len - off;
133
1.18G
        if (want >= diff) {
134
1.18G
            want -= diff;
135
136
1.18G
            if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
137
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
138
0
                goto err;
139
0
            }
140
1.18G
            i = BIO_read(in, &(b->data[len]), want);
141
1.18G
            if (i <= 0) {
142
                /*
143
                 * A read error (i < 0), an EOF in the middle of an object
144
                 * (diff != 0, some bytes already buffered), or an EOF while
145
                 * still inside an indefinite-length constructed value awaiting
146
                 * its end-of-contents octets (eos != 0) all mean the input is
147
                 * truncated.  Only a clean EOF at a top-level object boundary
148
                 * (i == 0, diff == 0, eos == 0) is the normal end of input:
149
                 * fail without queuing an error so that callers looping over
150
                 * concatenated DER values (e.g. the libcrypto d2i_*_bio()
151
                 * consumers in CPython's ssl module) terminate cleanly instead
152
                 * of seeing a spurious ASN1_R_NOT_ENOUGH_DATA.
153
                 */
154
214k
                if (i < 0 || diff != 0 || eos != 0)
155
214k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
156
214k
                goto err;
157
214k
            }
158
1.18G
            if (i > 0) {
159
1.18G
                if (len + i < len) {
160
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
161
0
                    goto err;
162
0
                }
163
1.18G
                len += i;
164
1.18G
                if ((size_t)i < want)
165
69.2k
                    continue;
166
1.18G
            }
167
1.18G
        }
168
        /* else data already loaded */
169
170
        /* make sure there is enough data for a complete header */
171
1.18G
        p = (unsigned char *)&(b->data[off]);
172
1.18G
        q = p;
173
1.18G
        diff = len - off;
174
1.18G
        if (diff < 2) {
175
            /* Failed sanity check */
176
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
177
0
            goto err;
178
0
        }
179
180
1.18G
        diff--;
181
1.18G
        if ((*(q++) & V_ASN1_PRIMITIVE_TAG) == V_ASN1_PRIMITIVE_TAG) {
182
25.0M
            unsigned int n = 0;
183
            /* Multi-byte tag.  See if we have the whole thing yet */
184
29.8M
            do {
185
29.8M
                if (n > 4) {
186
                    /* The tag value must fit into int */
187
17.9k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
188
17.9k
                    goto err;
189
17.9k
                }
190
29.8M
                ++n;
191
29.8M
                diff--;
192
29.8M
            } while (diff > 0 && *(q++) & 0x80);
193
194
24.9M
            if (diff == 0) {
195
                /*
196
                 * End of current data, will need at least 1 more byte for
197
                 * length.  2 if the tag is still incomplete
198
                 */
199
12.7M
                want = q - p + 2;
200
12.7M
                if (*q & 0x80) {
201
3.41M
                    want++;
202
3.41M
                }
203
12.7M
                continue;
204
12.7M
            }
205
24.9M
        }
206
207
        /* Check the length.  This should also work for indefinite length */
208
1.17G
        diff--;
209
1.17G
        if (*q & 0x80) {
210
351M
            unsigned int n = *q & 0x7f;
211
212
351M
            if (n > sizeof(long)) {
213
77.1k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
214
77.1k
                goto err;
215
77.1k
            }
216
351M
            if (n > diff) {
217
13.8M
                want = q - p + n + 1;
218
13.8M
                continue;
219
13.8M
            }
220
351M
        }
221
222
        /*
223
         * We have a complete header now, assuming we didn't hit EOF. Parse the
224
         * tag and length
225
         */
226
1.16G
        q = p;
227
1.16G
        diff = len - off;
228
1.16G
        inf = ASN1_get_object(&q, &slen, &tag, &xclass, (int)diff);
229
1.16G
        if (inf & 0x80) {
230
353M
            unsigned long e;
231
232
353M
            e = ERR_GET_REASON(ERR_peek_last_error());
233
353M
            if (e != ASN1_R_TOO_LONG)
234
22.1k
                goto err;
235
353M
            ERR_pop_to_mark();
236
353M
            ERR_set_mark();
237
353M
        }
238
1.16G
        off += q - p; /* end of data */
239
240
1.16G
        if (inf & 1) {
241
            /* no data body so go round again */
242
324M
            if (eos == UINT32_MAX) {
243
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
244
0
                goto err;
245
0
            }
246
324M
            eos++;
247
324M
            want = HEADER_SIZE;
248
838M
        } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
249
            /* eos value, so go back and read another header */
250
89.5M
            eos--;
251
89.5M
            if (eos == 0)
252
2.56M
                break;
253
86.9M
            else
254
86.9M
                want = HEADER_SIZE;
255
748M
        } else {
256
            /* suck in slen bytes of data */
257
748M
            want = slen;
258
748M
            if (want > (len - off)) {
259
353M
                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
260
261
353M
                want -= (len - off);
262
353M
                if (want > INT_MAX /* BIO_read takes an int length */ || len + want < len) {
263
40.9k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
264
40.9k
                    goto err;
265
40.9k
                }
266
706M
                while (want > 0) {
267
                    /*
268
                     * Read content in chunks of increasing size
269
                     * so we can return an error for EOF without
270
                     * having to allocate the entire content length
271
                     * in one go.
272
                     */
273
353M
                    size_t chunk = want > chunk_max ? chunk_max : want;
274
275
353M
                    if (!BUF_MEM_grow_clean(b, len + chunk)) {
276
0
                        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
277
0
                        goto err;
278
0
                    }
279
353M
                    want -= chunk;
280
707M
                    while (chunk > 0) {
281
353M
                        i = BIO_read(in, &(b->data[len]), chunk);
282
353M
                        if (i <= 0) {
283
201k
                            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
284
201k
                            goto err;
285
201k
                        }
286
                        /*
287
                         * This can't overflow because |len+want| didn't
288
                         * overflow.
289
                         */
290
353M
                        len += i;
291
353M
                        chunk -= i;
292
353M
                    }
293
353M
                    if (chunk_max < INT_MAX / 2)
294
353M
                        chunk_max *= 2;
295
353M
                }
296
353M
            }
297
748M
            if (off + slen < off) {
298
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
299
0
                goto err;
300
0
            }
301
748M
            off += slen;
302
748M
            if (eos == 0) {
303
1.88M
                break;
304
1.88M
            } else
305
746M
                want = HEADER_SIZE;
306
748M
        }
307
1.16G
    }
308
309
4.44M
    if (off > INT_MAX) {
310
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
311
0
        goto err;
312
0
    }
313
314
4.44M
    *pb = b;
315
4.44M
    ERR_clear_last_mark();
316
4.44M
    return off;
317
573k
err:
318
573k
    ERR_clear_last_mark();
319
573k
    BUF_MEM_free(b);
320
573k
    return -1;
321
4.44M
}