Coverage Report

Created: 2026-10-28 06:28

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