Coverage Report

Created: 2025-08-28 07:07

/src/openssl/crypto/asn1/a_d2i_fp.c
Line
Count
Source (jump to first uncovered line)
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
114k
{
61
114k
    BUF_MEM *b = NULL;
62
114k
    const unsigned char *p;
63
114k
    void *ret = NULL;
64
114k
    int len;
65
66
114k
    if (in == NULL)
67
0
        return NULL;
68
114k
    len = asn1_d2i_read_bio(in, &b);
69
114k
    if (len < 0)
70
9.00k
        goto err;
71
72
105k
    p = (const unsigned char *)b->data;
73
105k
    ret = ASN1_item_d2i_ex(x, &p, len, it, libctx, propq);
74
114k
 err:
75
114k
    BUF_MEM_free(b);
76
114k
    return ret;
77
105k
}
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.73G
#define HEADER_SIZE   8
108
161M
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
109
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
110
5.19M
{
111
5.19M
    BUF_MEM *b;
112
5.19M
    unsigned char *p;
113
5.19M
    size_t want = HEADER_SIZE;
114
5.19M
    uint32_t eos = 0;
115
5.19M
    size_t off = 0;
116
5.19M
    size_t len = 0;
117
5.19M
    size_t diff;
118
119
5.19M
    const unsigned char *q;
120
5.19M
    long slen;
121
5.19M
    int inf, tag, xclass;
122
123
5.19M
    b = BUF_MEM_new();
124
5.19M
    if (b == NULL) {
125
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
126
0
        return -1;
127
0
    }
128
129
5.19M
    ERR_set_mark();
130
1.73G
    for (;;) {
131
1.73G
        diff = len - off;
132
1.73G
        if (want >= diff) {
133
1.73G
            int i;
134
135
1.73G
            want -= diff;
136
137
1.73G
            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.73G
            i = BIO_read(in, &(b->data[len]), (int)want);
142
1.73G
            if (i < 0 && diff == 0) {
143
1.55k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
144
1.55k
                goto err;
145
1.55k
            }
146
1.73G
            if (i > 0) {
147
1.72G
                if (len + i < len) {
148
0
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
149
0
                    goto err;
150
0
                }
151
1.72G
                len += i;
152
1.72G
                if ((size_t)i < want)
153
1.81M
                    continue;
154
155
1.72G
            }
156
1.73G
        }
157
        /* else data already loaded */
158
159
1.73G
        p = (unsigned char *)&(b->data[off]);
160
1.73G
        q = p;
161
1.73G
        diff = len - off;
162
1.73G
        if (diff == 0)
163
175k
            goto err;
164
1.73G
        inf = ASN1_get_object(&q, &slen, &tag, &xclass, (int)diff);
165
1.73G
        if (inf & 0x80) {
166
162M
            unsigned long e;
167
168
162M
            e = ERR_GET_REASON(ERR_peek_last_error());
169
162M
            if (e != ASN1_R_TOO_LONG)
170
234k
                goto err;
171
161M
            ERR_pop_to_mark();
172
161M
        }
173
1.73G
        off += q - p;               /* end of data */
174
175
1.73G
        if (inf & 1) {
176
            /* no data body so go round again */
177
433M
            if (eos == UINT32_MAX) {
178
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
179
0
                goto err;
180
0
            }
181
433M
            eos++;
182
433M
            want = HEADER_SIZE;
183
1.29G
        } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
184
            /* eos value, so go back and read another header */
185
115M
            eos--;
186
115M
            if (eos == 0)
187
2.39M
                break;
188
112M
            else
189
112M
                want = HEADER_SIZE;
190
1.18G
        } else {
191
            /* suck in slen bytes of data */
192
1.18G
            want = slen;
193
1.18G
            if (want > (len - off)) {
194
161M
                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
195
196
161M
                want -= (len - off);
197
161M
                if (want > INT_MAX /* BIO_read takes an int length */  ||
198
161M
                    len + want < len) {
199
17.9k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
200
17.9k
                    goto err;
201
17.9k
                }
202
323M
                while (want > 0) {
203
                    /*
204
                     * Read content in chunks of increasing size
205
                     * so we can return an error for EOF without
206
                     * having to allocate the entire content length
207
                     * in one go.
208
                     */
209
162M
                    size_t chunk = want > chunk_max ? chunk_max : want;
210
162M
                    int i;
211
212
162M
                    if (!BUF_MEM_grow_clean(b, len + chunk)) {
213
0
                        ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
214
0
                        goto err;
215
0
                    }
216
162M
                    want -= chunk;
217
324M
                    while (chunk > 0) {
218
162M
                        i = BIO_read(in, &(b->data[len]), (int)chunk);
219
162M
                        if (i <= 0) {
220
237k
                            ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
221
237k
                            goto err;
222
237k
                        }
223
                    /*
224
                     * This can't overflow because |len+want| didn't
225
                     * overflow.
226
                     */
227
162M
                        len += i;
228
162M
                        chunk -= i;
229
162M
                    }
230
161M
                    if (chunk_max < INT_MAX/2)
231
161M
                        chunk_max *= 2;
232
161M
                }
233
161M
            }
234
1.18G
            if (off + slen < off) {
235
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
236
0
                goto err;
237
0
            }
238
1.18G
            off += slen;
239
1.18G
            if (eos == 0) {
240
2.13M
                break;
241
2.13M
            } else
242
1.17G
                want = HEADER_SIZE;
243
1.18G
        }
244
1.73G
    }
245
246
4.52M
    if (off > INT_MAX) {
247
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
248
0
        goto err;
249
0
    }
250
251
4.52M
    *pb = b;
252
4.52M
    return (int)off;
253
667k
 err:
254
667k
    ERR_clear_last_mark();
255
667k
    BUF_MEM_free(b);
256
667k
    return -1;
257
4.52M
}