Coverage Report

Created: 2025-12-31 06:58

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