Coverage Report

Created: 2023-06-08 06:43

/src/openssl111/crypto/asn1/a_d2i_fp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 "crypto/asn1.h"
17
18
#ifndef NO_OLD_ASN1
19
# ifndef OPENSSL_NO_STDIO
20
21
void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x)
22
0
{
23
0
    BIO *b;
24
0
    void *ret;
25
26
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
27
0
        ASN1err(ASN1_F_ASN1_D2I_FP, ERR_R_BUF_LIB);
28
0
        return NULL;
29
0
    }
30
0
    BIO_set_fp(b, in, BIO_NOCLOSE);
31
0
    ret = ASN1_d2i_bio(xnew, d2i, b, x);
32
0
    BIO_free(b);
33
0
    return ret;
34
0
}
35
# endif
36
37
void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x)
38
0
{
39
0
    BUF_MEM *b = NULL;
40
0
    const unsigned char *p;
41
0
    void *ret = NULL;
42
0
    int len;
43
44
0
    len = asn1_d2i_read_bio(in, &b);
45
0
    if (len < 0)
46
0
        goto err;
47
48
0
    p = (unsigned char *)b->data;
49
0
    ret = d2i(x, &p, len);
50
0
 err:
51
0
    BUF_MEM_free(b);
52
0
    return ret;
53
0
}
54
55
#endif
56
57
void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
58
8.17k
{
59
8.17k
    BUF_MEM *b = NULL;
60
8.17k
    const unsigned char *p;
61
8.17k
    void *ret = NULL;
62
8.17k
    int len;
63
64
8.17k
    len = asn1_d2i_read_bio(in, &b);
65
8.17k
    if (len < 0)
66
471
        goto err;
67
68
7.70k
    p = (const unsigned char *)b->data;
69
7.70k
    ret = ASN1_item_d2i(x, &p, len, it);
70
8.17k
 err:
71
8.17k
    BUF_MEM_free(b);
72
8.17k
    return ret;
73
7.70k
}
74
75
#ifndef OPENSSL_NO_STDIO
76
void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
77
0
{
78
0
    BIO *b;
79
0
    char *ret;
80
81
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
82
0
        ASN1err(ASN1_F_ASN1_ITEM_D2I_FP, ERR_R_BUF_LIB);
83
0
        return NULL;
84
0
    }
85
0
    BIO_set_fp(b, in, BIO_NOCLOSE);
86
0
    ret = ASN1_item_d2i_bio(it, b, x);
87
0
    BIO_free(b);
88
0
    return ret;
89
0
}
90
#endif
91
92
13.5M
#define HEADER_SIZE   8
93
645k
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
94
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
95
8.17k
{
96
8.17k
    BUF_MEM *b;
97
8.17k
    unsigned char *p;
98
8.17k
    int i;
99
8.17k
    size_t want = HEADER_SIZE;
100
8.17k
    uint32_t eos = 0;
101
8.17k
    size_t off = 0;
102
8.17k
    size_t len = 0;
103
104
8.17k
    const unsigned char *q;
105
8.17k
    long slen;
106
8.17k
    int inf, tag, xclass;
107
108
8.17k
    b = BUF_MEM_new();
109
8.17k
    if (b == NULL) {
110
0
        ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
111
0
        return -1;
112
0
    }
113
114
8.17k
    ERR_clear_error();
115
13.5M
    for (;;) {
116
13.5M
        if (want >= (len - off)) {
117
13.5M
            want -= (len - off);
118
119
13.5M
            if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
120
0
                ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
121
0
                goto err;
122
0
            }
123
13.5M
            i = BIO_read(in, &(b->data[len]), want);
124
13.5M
            if ((i < 0) && ((len - off) == 0)) {
125
97
                ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA);
126
97
                goto err;
127
97
            }
128
13.5M
            if (i > 0) {
129
13.5M
                if (len + i < len) {
130
0
                    ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
131
0
                    goto err;
132
0
                }
133
13.5M
                len += i;
134
13.5M
            }
135
13.5M
        }
136
        /* else data already loaded */
137
138
13.5M
        p = (unsigned char *)&(b->data[off]);
139
13.5M
        q = p;
140
13.5M
        inf = ASN1_get_object(&q, &slen, &tag, &xclass, len - off);
141
13.5M
        if (inf & 0x80) {
142
645k
            unsigned long e;
143
144
645k
            e = ERR_GET_REASON(ERR_peek_error());
145
645k
            if (e != ASN1_R_TOO_LONG)
146
72
                goto err;
147
645k
            else
148
645k
                ERR_clear_error(); /* clear error */
149
645k
        }
150
13.5M
        i = q - p;            /* header length */
151
13.5M
        off += i;               /* end of data */
152
153
13.5M
        if (inf & 1) {
154
            /* no data body so go round again */
155
1.23M
            if (eos == UINT32_MAX) {
156
0
                ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG);
157
0
                goto err;
158
0
            }
159
1.23M
            eos++;
160
1.23M
            want = HEADER_SIZE;
161
12.3M
        } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
162
            /* eos value, so go back and read another header */
163
523k
            eos--;
164
523k
            if (eos == 0)
165
5.07k
                break;
166
518k
            else
167
518k
                want = HEADER_SIZE;
168
11.7M
        } else {
169
            /* suck in slen bytes of data */
170
11.7M
            want = slen;
171
11.7M
            if (want > (len - off)) {
172
645k
                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
173
174
645k
                want -= (len - off);
175
645k
                if (want > INT_MAX /* BIO_read takes an int length */  ||
176
645k
                    len + want < len) {
177
41
                    ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
178
41
                    goto err;
179
41
                }
180
1.29M
                while (want > 0) {
181
                    /*
182
                     * Read content in chunks of increasing size
183
                     * so we can return an error for EOF without
184
                     * having to allocate the entire content length
185
                     * in one go.
186
                     */
187
645k
                    size_t chunk = want > chunk_max ? chunk_max : want;
188
189
645k
                    if (!BUF_MEM_grow_clean(b, len + chunk)) {
190
0
                        ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
191
0
                        goto err;
192
0
                    }
193
645k
                    want -= chunk;
194
1.29M
                    while (chunk > 0) {
195
645k
                        i = BIO_read(in, &(b->data[len]), chunk);
196
645k
                        if (i <= 0) {
197
261
                            ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
198
261
                                    ASN1_R_NOT_ENOUGH_DATA);
199
261
                            goto err;
200
261
                        }
201
                    /*
202
                     * This can't overflow because |len+want| didn't
203
                     * overflow.
204
                     */
205
645k
                        len += i;
206
645k
                        chunk -= i;
207
645k
                    }
208
645k
                    if (chunk_max < INT_MAX/2)
209
645k
                        chunk_max *= 2;
210
645k
                }
211
645k
            }
212
11.7M
            if (off + slen < off) {
213
0
                ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
214
0
                goto err;
215
0
            }
216
11.7M
            off += slen;
217
11.7M
            if (eos == 0) {
218
2.63k
                break;
219
2.63k
            } else
220
11.7M
                want = HEADER_SIZE;
221
11.7M
        }
222
13.5M
    }
223
224
7.70k
    if (off > INT_MAX) {
225
0
        ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
226
0
        goto err;
227
0
    }
228
229
7.70k
    *pb = b;
230
7.70k
    return off;
231
471
 err:
232
471
    BUF_MEM_free(b);
233
471
    return -1;
234
7.70k
}