Coverage Report

Created: 2026-09-12 06:55

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
102k
{
61
102k
    BUF_MEM *b = NULL;
62
102k
    const unsigned char *p;
63
102k
    void *ret = NULL;
64
102k
    int len;
65
66
102k
    if (in == NULL)
67
0
        return NULL;
68
102k
    len = asn1_d2i_read_bio(in, &b);
69
102k
    if (len < 0)
70
11.3k
        goto err;
71
72
91.4k
    p = (const unsigned char *)b->data;
73
91.4k
    ret = ASN1_item_d2i_ex(x, &p, len, it, libctx, propq);
74
102k
err:
75
102k
    BUF_MEM_free(b);
76
102k
    return ret;
77
91.4k
}
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.06G
#define HEADER_SIZE 2
108
257M
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
109
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
110
4.78M
{
111
4.78M
    BUF_MEM *b;
112
4.78M
    unsigned char *p;
113
4.78M
    int i;
114
4.78M
    size_t want = HEADER_SIZE;
115
4.78M
    uint32_t eos = 0;
116
4.78M
    size_t off = 0;
117
4.78M
    size_t len = 0;
118
4.78M
    size_t diff;
119
120
4.78M
    const unsigned char *q;
121
4.78M
    long slen;
122
4.78M
    int inf, tag, xclass;
123
124
4.78M
    b = BUF_MEM_new();
125
4.78M
    if (b == NULL) {
126
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
127
0
        return -1;
128
0
    }
129
130
4.78M
    ERR_set_mark();
131
1.08G
    for (;;) {
132
1.08G
        diff = len - off;
133
1.08G
        if (want >= diff) {
134
1.08G
            want -= diff;
135
136
1.08G
            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.08G
            i = BIO_read(in, &(b->data[len]), want);
141
1.08G
            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
206k
                if (i < 0 || diff != 0 || eos != 0)
155
206k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
156
206k
                goto err;
157
206k
            }
158
1.08G
            if (i > 0) {
159
1.08G
                if (len + i < len) {
160
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
161
0
                    goto err;
162
0
                }
163
1.08G
                len += i;
164
1.08G
                if ((size_t)i < want)
165
65.0k
                    continue;
166
1.08G
            }
167
1.08G
        }
168
        /* else data already loaded */
169
170
        /* make sure there is enough data for a complete header */
171
1.08G
        p = (unsigned char *)&(b->data[off]);
172
1.08G
        q = p;
173
1.08G
        diff = len - off;
174
1.08G
        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.08G
        diff--;
181
1.08G
        if ((*(q++) & V_ASN1_PRIMITIVE_TAG) == V_ASN1_PRIMITIVE_TAG) {
182
18.4M
            unsigned int n = 0;
183
            /* Multi-byte tag.  See if we have the whole thing yet */
184
22.8M
            do {
185
22.8M
                if (n > 4) {
186
                    /* The tag value must fit into int */
187
21.0k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
188
21.0k
                    goto err;
189
21.0k
                }
190
22.7M
                ++n;
191
22.7M
                diff--;
192
22.7M
            } while (diff > 0 && *(q++) & 0x80);
193
194
18.4M
            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
9.51M
                want = q - p + 2;
200
9.51M
                if (*q & 0x80) {
201
2.46M
                    want++;
202
2.46M
                }
203
9.51M
                continue;
204
9.51M
            }
205
18.4M
        }
206
207
        /* Check the length.  This should also work for indefinite length */
208
1.07G
        diff--;
209
1.07G
        if (*q & 0x80) {
210
370M
            unsigned int n = *q & 0x7f;
211
212
370M
            if (n > sizeof(long)) {
213
68.3k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
214
68.3k
                goto err;
215
68.3k
            }
216
370M
            if (n > diff) {
217
13.5M
                want = q - p + n + 1;
218
13.5M
                continue;
219
13.5M
            }
220
370M
        }
221
222
        /*
223
         * We have a complete header now, assuming we didn't hit EOF. Parse the
224
         * tag and length
225
         */
226
1.05G
        q = p;
227
1.05G
        diff = len - off;
228
1.05G
        inf = ASN1_get_object(&q, &slen, &tag, &xclass, (int)diff);
229
1.05G
        if (inf & 0x80) {
230
257M
            unsigned long e;
231
232
257M
            e = ERR_GET_REASON(ERR_peek_last_error());
233
257M
            if (e != ASN1_R_TOO_LONG)
234
23.4k
                goto err;
235
257M
            ERR_pop_to_mark();
236
257M
            ERR_set_mark();
237
257M
        }
238
1.05G
        off += q - p; /* end of data */
239
240
1.05G
        if (inf & 1) {
241
            /* no data body so go round again */
242
343M
            if (eos == UINT32_MAX) {
243
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
244
0
                goto err;
245
0
            }
246
343M
            eos++;
247
343M
            want = HEADER_SIZE;
248
716M
        } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
249
            /* eos value, so go back and read another header */
250
72.9M
            eos--;
251
72.9M
            if (eos == 0)
252
2.38M
                break;
253
70.6M
            else
254
70.6M
                want = HEADER_SIZE;
255
643M
        } else {
256
            /* suck in slen bytes of data */
257
643M
            want = slen;
258
643M
            if (want > (len - off)) {
259
257M
                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
260
261
257M
                want -= (len - off);
262
257M
                if (want > INT_MAX /* BIO_read takes an int length */ || len + want < len) {
263
47.8k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
264
47.8k
                    goto err;
265
47.8k
                }
266
515M
                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
257M
                    size_t chunk = want > chunk_max ? chunk_max : want;
274
275
257M
                    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
257M
                    want -= chunk;
280
515M
                    while (chunk > 0) {
281
257M
                        i = BIO_read(in, &(b->data[len]), chunk);
282
257M
                        if (i <= 0) {
283
172k
                            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
284
172k
                            goto err;
285
172k
                        }
286
                        /*
287
                         * This can't overflow because |len+want| didn't
288
                         * overflow.
289
                         */
290
257M
                        len += i;
291
257M
                        chunk -= i;
292
257M
                    }
293
257M
                    if (chunk_max < INT_MAX / 2)
294
257M
                        chunk_max *= 2;
295
257M
                }
296
257M
            }
297
643M
            if (off + slen < off) {
298
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
299
0
                goto err;
300
0
            }
301
643M
            off += slen;
302
643M
            if (eos == 0) {
303
1.85M
                break;
304
1.85M
            } else
305
641M
                want = HEADER_SIZE;
306
643M
        }
307
1.05G
    }
308
309
4.24M
    if (off > INT_MAX) {
310
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
311
0
        goto err;
312
0
    }
313
314
4.24M
    *pb = b;
315
4.24M
    ERR_clear_last_mark();
316
4.24M
    return off;
317
540k
err:
318
540k
    ERR_clear_last_mark();
319
540k
    BUF_MEM_free(b);
320
540k
    return -1;
321
4.24M
}