Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/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
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
432M
#define HEADER_SIZE 8
108
20.3M
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
109
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
110
870k
{
111
870k
    BUF_MEM *b;
112
870k
    unsigned char *p;
113
870k
    int i;
114
870k
    size_t want = HEADER_SIZE;
115
870k
    uint32_t eos = 0;
116
870k
    size_t off = 0;
117
870k
    size_t len = 0;
118
870k
    size_t diff;
119
120
870k
    const unsigned char *q;
121
870k
    long slen;
122
870k
    int inf, tag, xclass;
123
124
870k
    b = BUF_MEM_new();
125
870k
    if (b == NULL) {
126
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
127
0
        return -1;
128
0
    }
129
130
870k
    ERR_set_mark();
131
432M
    for (;;) {
132
432M
        diff = len - off;
133
432M
        if (want >= diff) {
134
432M
            want -= diff;
135
136
432M
            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
432M
            i = BIO_read(in, &(b->data[len]), want);
141
432M
            if (i < 0 && diff == 0) {
142
198
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
143
198
                goto err;
144
198
            }
145
432M
            if (i > 0) {
146
431M
                if (len + i < len) {
147
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
148
0
                    goto err;
149
0
                }
150
431M
                len += i;
151
431M
                if ((size_t)i < want)
152
283k
                    continue;
153
431M
            }
154
432M
        }
155
        /* else data already loaded */
156
157
432M
        p = (unsigned char *)&(b->data[off]);
158
432M
        q = p;
159
432M
        diff = len - off;
160
432M
        if (diff == 0)
161
18.4k
            goto err;
162
432M
        inf = ASN1_get_object(&q, &slen, &tag, &xclass, diff);
163
432M
        if (inf & 0x80) {
164
20.4M
            unsigned long e;
165
166
20.4M
            e = ERR_GET_REASON(ERR_peek_last_error());
167
20.4M
            if (e != ASN1_R_TOO_LONG)
168
28.5k
                goto err;
169
20.3M
            ERR_pop_to_mark();
170
20.3M
            ERR_set_mark();
171
20.3M
        }
172
432M
        i = q - p; /* header length */
173
432M
        off += i; /* end of data */
174
175
432M
        if (inf & 1) {
176
            /* no data body so go round again */
177
118M
            if (eos == UINT32_MAX) {
178
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
179
0
                goto err;
180
0
            }
181
118M
            eos++;
182
118M
            want = HEADER_SIZE;
183
313M
        } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
184
            /* eos value, so go back and read another header */
185
33.8M
            eos--;
186
33.8M
            if (eos == 0)
187
415k
                break;
188
33.4M
            else
189
33.4M
                want = HEADER_SIZE;
190
279M
        } else {
191
            /* suck in slen bytes of data */
192
279M
            want = slen;
193
279M
            if (want > (len - off)) {
194
20.3M
                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
195
196
20.3M
                want -= (len - off);
197
20.3M
                if (want > INT_MAX /* BIO_read takes an int length */ || len + want < len) {
198
2.11k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
199
2.11k
                    goto err;
200
2.11k
                }
201
40.7M
                while (want > 0) {
202
                    /*
203
                     * Read content in chunks of increasing size
204
                     * so we can return an error for EOF without
205
                     * having to allocate the entire content length
206
                     * in one go.
207
                     */
208
20.4M
                    size_t chunk = want > chunk_max ? chunk_max : want;
209
210
20.4M
                    if (!BUF_MEM_grow_clean(b, len + chunk)) {
211
0
                        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
212
0
                        goto err;
213
0
                    }
214
20.4M
                    want -= chunk;
215
40.8M
                    while (chunk > 0) {
216
20.4M
                        i = BIO_read(in, &(b->data[len]), chunk);
217
20.4M
                        if (i <= 0) {
218
32.7k
                            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
219
32.7k
                            goto err;
220
32.7k
                        }
221
                        /*
222
                         * This can't overflow because |len+want| didn't
223
                         * overflow.
224
                         */
225
20.4M
                        len += i;
226
20.4M
                        chunk -= i;
227
20.4M
                    }
228
20.3M
                    if (chunk_max < INT_MAX / 2)
229
20.3M
                        chunk_max *= 2;
230
20.3M
                }
231
20.3M
            }
232
279M
            if (off + slen < off) {
233
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
234
0
                goto err;
235
0
            }
236
279M
            off += slen;
237
279M
            if (eos == 0) {
238
372k
                break;
239
372k
            } else
240
279M
                want = HEADER_SIZE;
241
279M
        }
242
432M
    }
243
244
788k
    if (off > INT_MAX) {
245
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
246
0
        goto err;
247
0
    }
248
249
788k
    *pb = b;
250
788k
    ERR_clear_last_mark();
251
788k
    return off;
252
82.1k
err:
253
82.1k
    ERR_clear_last_mark();
254
82.1k
    BUF_MEM_free(b);
255
82.1k
    return -1;
256
788k
}