Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/asn1/a_d2i_fp.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
130k
{
61
130k
    BUF_MEM *b = NULL;
62
130k
    const unsigned char *p;
63
130k
    void *ret = NULL;
64
130k
    int len;
65
66
130k
    if (in == NULL)
67
0
        return NULL;
68
130k
    len = asn1_d2i_read_bio(in, &b);
69
130k
    if (len < 0)
70
11.4k
        goto err;
71
72
119k
    p = (const unsigned char *)b->data;
73
119k
    ret = ASN1_item_d2i_ex(x, &p, len, it, libctx, propq);
74
130k
err:
75
130k
    BUF_MEM_free(b);
76
130k
    return ret;
77
119k
}
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.54G
#define HEADER_SIZE 2
108
437M
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
109
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
110
5.58M
{
111
5.58M
    BUF_MEM *b;
112
5.58M
    unsigned char *p;
113
5.58M
    int i;
114
5.58M
    size_t want = HEADER_SIZE;
115
5.58M
    uint32_t eos = 0;
116
5.58M
    size_t off = 0;
117
5.58M
    size_t len = 0;
118
5.58M
    size_t diff;
119
120
5.58M
    const unsigned char *q;
121
5.58M
    long slen;
122
5.58M
    int inf, tag, xclass;
123
124
5.58M
    b = BUF_MEM_new();
125
5.58M
    if (b == NULL) {
126
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
127
0
        return -1;
128
0
    }
129
130
5.58M
    ERR_set_mark();
131
1.57G
    for (;;) {
132
1.57G
        diff = len - off;
133
1.57G
        if (want >= diff) {
134
1.57G
            want -= diff;
135
136
1.57G
            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.57G
            i = BIO_read(in, &(b->data[len]), want);
141
1.57G
            if (i <= 0) {
142
264k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
143
264k
                goto err;
144
264k
            }
145
1.57G
            if (i > 0) {
146
1.57G
                if (len + i < len) {
147
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
148
0
                    goto err;
149
0
                }
150
1.57G
                len += i;
151
1.57G
                if ((size_t)i < want)
152
79.9k
                    continue;
153
1.57G
            }
154
1.57G
        }
155
        /* else data already loaded */
156
157
        /* make sure there is enough data for a complete header */
158
1.57G
        p = (unsigned char *)&(b->data[off]);
159
1.57G
        q = p;
160
1.57G
        diff = len - off;
161
1.57G
        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.57G
        diff--;
168
1.57G
        if ((*(q++) & V_ASN1_PRIMITIVE_TAG) == V_ASN1_PRIMITIVE_TAG) {
169
23.6M
            unsigned int n = 0;
170
            /* Multi-byte tag.  See if we have the whole thing yet */
171
28.8M
            do {
172
28.8M
                if (n > 4) {
173
                    /* The tag value must fit into int */
174
14.1k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
175
14.1k
                    goto err;
176
14.1k
                }
177
28.8M
                ++n;
178
28.8M
                diff--;
179
28.8M
            } while (diff > 0 && *(q++) & 0x80);
180
181
23.6M
            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
12.0M
                want = q - p + 2;
187
12.0M
                if (*q & 0x80) {
188
3.66M
                    want++;
189
3.66M
                }
190
12.0M
                continue;
191
12.0M
            }
192
23.6M
        }
193
194
        /* Check the length.  This should also work for indefinite length */
195
1.56G
        diff--;
196
1.56G
        if (*q & 0x80) {
197
531M
            unsigned int n = *q & 0x7f;
198
199
531M
            if (n > sizeof(long)) {
200
82.9k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
201
82.9k
                goto err;
202
82.9k
            }
203
531M
            if (n > diff) {
204
20.0M
                want = q - p + n + 1;
205
20.0M
                continue;
206
20.0M
            }
207
531M
        }
208
209
        /*
210
         * We have a complete header now, assuming we didn't hit EOF. Parse the
211
         * tag and length
212
         */
213
1.54G
        q = p;
214
1.54G
        diff = len - off;
215
1.54G
        inf = ASN1_get_object(&q, &slen, &tag, &xclass, (int)diff);
216
1.54G
        if (inf & 0x80) {
217
437M
            unsigned long e;
218
219
437M
            e = ERR_GET_REASON(ERR_peek_last_error());
220
437M
            if (e != ASN1_R_TOO_LONG)
221
23.2k
                goto err;
222
437M
            ERR_pop_to_mark();
223
437M
            ERR_set_mark();
224
437M
        }
225
1.54G
        off += q - p; /* end of data */
226
227
1.54G
        if (inf & 1) {
228
            /* no data body so go round again */
229
491M
            if (eos == UINT32_MAX) {
230
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
231
0
                goto err;
232
0
            }
233
491M
            eos++;
234
491M
            want = HEADER_SIZE;
235
1.05G
        } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
236
            /* eos value, so go back and read another header */
237
115M
            eos--;
238
115M
            if (eos == 0)
239
2.87M
                break;
240
112M
            else
241
112M
                want = HEADER_SIZE;
242
935M
        } else {
243
            /* suck in slen bytes of data */
244
935M
            want = slen;
245
935M
            if (want > (len - off)) {
246
437M
                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
247
248
437M
                want -= (len - off);
249
437M
                if (want > INT_MAX /* BIO_read takes an int length */ || len + want < len) {
250
48.1k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
251
48.1k
                    goto err;
252
48.1k
                }
253
874M
                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
437M
                    size_t chunk = want > chunk_max ? chunk_max : want;
261
262
437M
                    if (!BUF_MEM_grow_clean(b, len + chunk)) {
263
0
                        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
264
0
                        goto err;
265
0
                    }
266
437M
                    want -= chunk;
267
875M
                    while (chunk > 0) {
268
437M
                        i = BIO_read(in, &(b->data[len]), chunk);
269
437M
                        if (i <= 0) {
270
222k
                            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
271
222k
                            goto err;
272
222k
                        }
273
                        /*
274
                         * This can't overflow because |len+want| didn't
275
                         * overflow.
276
                         */
277
437M
                        len += i;
278
437M
                        chunk -= i;
279
437M
                    }
280
437M
                    if (chunk_max < INT_MAX / 2)
281
437M
                        chunk_max *= 2;
282
437M
                }
283
437M
            }
284
934M
            if (off + slen < off) {
285
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
286
0
                goto err;
287
0
            }
288
934M
            off += slen;
289
934M
            if (eos == 0) {
290
2.04M
                break;
291
2.04M
            } else
292
932M
                want = HEADER_SIZE;
293
934M
        }
294
1.54G
    }
295
296
4.92M
    if (off > INT_MAX) {
297
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
298
0
        goto err;
299
0
    }
300
301
4.92M
    *pb = b;
302
4.92M
    ERR_clear_last_mark();
303
4.92M
    return off;
304
655k
err:
305
655k
    ERR_clear_last_mark();
306
655k
    BUF_MEM_free(b);
307
655k
    return -1;
308
4.92M
}