Coverage Report

Created: 2025-12-04 06:33

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