/src/openssl/crypto/asn1/a_d2i_fp.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 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 | 0 | { |
61 | 0 | BUF_MEM *b = NULL; |
62 | 0 | const unsigned char *p; |
63 | 0 | void *ret = NULL; |
64 | 0 | int len; |
65 | |
|
66 | 0 | if (in == NULL) |
67 | 0 | return NULL; |
68 | 0 | len = asn1_d2i_read_bio(in, &b); |
69 | 0 | if (len < 0) |
70 | 0 | goto err; |
71 | | |
72 | 0 | p = (const unsigned char *)b->data; |
73 | 0 | ret = ASN1_item_d2i_ex(x, &p, len, it, libctx, propq); |
74 | 0 | err: |
75 | 0 | BUF_MEM_free(b); |
76 | 0 | return ret; |
77 | 0 | } |
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 | void *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 | 0 | #define HEADER_SIZE 2 |
108 | 0 | #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024) |
109 | | int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb) |
110 | 0 | { |
111 | 0 | BUF_MEM *b; |
112 | 0 | unsigned char *p; |
113 | 0 | size_t want = HEADER_SIZE; |
114 | 0 | uint32_t eos = 0; |
115 | 0 | size_t off = 0; |
116 | 0 | size_t len = 0; |
117 | 0 | size_t diff; |
118 | |
|
119 | 0 | const unsigned char *q; |
120 | 0 | long slen; |
121 | 0 | int inf, tag, xclass; |
122 | |
|
123 | 0 | b = BUF_MEM_new(); |
124 | 0 | if (b == NULL) { |
125 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB); |
126 | 0 | return -1; |
127 | 0 | } |
128 | | |
129 | 0 | ERR_set_mark(); |
130 | 0 | for (;;) { |
131 | 0 | diff = len - off; |
132 | 0 | if (want >= diff) { |
133 | 0 | int i; |
134 | |
|
135 | 0 | want -= diff; |
136 | |
|
137 | 0 | 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 | 0 | i = BIO_read(in, &(b->data[len]), (int)want); |
142 | |
|
143 | 0 | if (i <= 0) { |
144 | | /* |
145 | | * A read error (i < 0), an EOF in the middle of an object |
146 | | * (diff != 0, some bytes already buffered), or an EOF while |
147 | | * still inside an indefinite-length constructed value awaiting |
148 | | * its end-of-contents octets (eos != 0) all mean the input is |
149 | | * truncated. Only a clean EOF at a top-level object boundary |
150 | | * (i == 0, diff == 0, eos == 0) is the normal end of input: |
151 | | * fail without queuing an error so that callers looping over |
152 | | * concatenated DER values (e.g. the libcrypto d2i_*_bio() |
153 | | * consumers in CPython's ssl module) terminate cleanly instead |
154 | | * of seeing a spurious ASN1_R_NOT_ENOUGH_DATA. |
155 | | */ |
156 | 0 | if (i < 0 || diff != 0 || eos != 0) |
157 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA); |
158 | 0 | goto err; |
159 | 0 | } |
160 | | |
161 | 0 | if (i > 0) { |
162 | 0 | if (len + i < len) { |
163 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG); |
164 | 0 | goto err; |
165 | 0 | } |
166 | 0 | len += i; |
167 | 0 | if ((size_t)i < want) |
168 | 0 | continue; |
169 | 0 | } |
170 | 0 | } |
171 | | /* else data already loaded */ |
172 | | |
173 | | /* make sure there is enough data for a complete header */ |
174 | 0 | p = (unsigned char *)&(b->data[off]); |
175 | 0 | q = p; |
176 | 0 | diff = len - off; |
177 | 0 | if (diff < 2) { |
178 | | /* Failed sanity check */ |
179 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA); |
180 | 0 | goto err; |
181 | 0 | } |
182 | | |
183 | 0 | diff--; |
184 | 0 | if ((*(q++) & V_ASN1_PRIMITIVE_TAG) == V_ASN1_PRIMITIVE_TAG) { |
185 | 0 | unsigned int i = 0; |
186 | | /* Multi-byte tag. See if we have the whole thing yet */ |
187 | 0 | do { |
188 | 0 | if (i > 4) { |
189 | | /* The tag value must fit into int */ |
190 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG); |
191 | 0 | goto err; |
192 | 0 | } |
193 | 0 | ++i; |
194 | 0 | diff--; |
195 | 0 | } while (diff > 0 && *(q++) & 0x80); |
196 | | |
197 | 0 | if (diff == 0) { |
198 | | /* |
199 | | * End of current data, will need at least 1 more byte for |
200 | | * length. 2 if the tag is still incomplete |
201 | | */ |
202 | 0 | want = q - p + 2; |
203 | 0 | if (*q & 0x80) { |
204 | 0 | want++; |
205 | 0 | } |
206 | 0 | continue; |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | | /* Check the length. This should also work for indefinite length */ |
211 | 0 | diff--; |
212 | 0 | if (*q & 0x80) { |
213 | 0 | unsigned int i = *q & 0x7f; |
214 | |
|
215 | 0 | if (i > sizeof(long)) { |
216 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG); |
217 | 0 | goto err; |
218 | 0 | } |
219 | 0 | if (i > diff) { |
220 | 0 | want = q - p + i + 1; |
221 | 0 | continue; |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | | /* |
226 | | * We have a complete header now, assuming we didn't hit EOF. Parse the |
227 | | * tag and length |
228 | | */ |
229 | 0 | q = p; |
230 | 0 | diff = len - off; |
231 | 0 | inf = ASN1_get_object(&q, &slen, &tag, &xclass, (int)diff); |
232 | 0 | if (inf & 0x80) { |
233 | 0 | unsigned long e; |
234 | |
|
235 | 0 | e = ERR_GET_REASON(ERR_peek_last_error()); |
236 | 0 | if (e != ASN1_R_TOO_LONG) |
237 | 0 | goto err; |
238 | 0 | ERR_pop_to_mark(); |
239 | 0 | ERR_set_mark(); |
240 | 0 | } |
241 | 0 | off += q - p; /* end of data */ |
242 | |
|
243 | 0 | if (inf & 1) { |
244 | | /* no data body so go round again */ |
245 | 0 | if (eos == UINT32_MAX) { |
246 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG); |
247 | 0 | goto err; |
248 | 0 | } |
249 | 0 | eos++; |
250 | 0 | want = HEADER_SIZE; |
251 | 0 | } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) { |
252 | | /* eos value, so go back and read another header */ |
253 | 0 | eos--; |
254 | 0 | if (eos == 0) |
255 | 0 | break; |
256 | 0 | else |
257 | 0 | want = HEADER_SIZE; |
258 | 0 | } else { |
259 | | /* suck in slen bytes of data */ |
260 | 0 | want = slen; |
261 | 0 | if (want > (len - off)) { |
262 | 0 | size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE; |
263 | |
|
264 | 0 | want -= (len - off); |
265 | 0 | if (want > INT_MAX /* BIO_read takes an int length */ || len + want < len) { |
266 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG); |
267 | 0 | goto err; |
268 | 0 | } |
269 | 0 | while (want > 0) { |
270 | | /* |
271 | | * Read content in chunks of increasing size |
272 | | * so we can return an error for EOF without |
273 | | * having to allocate the entire content length |
274 | | * in one go. |
275 | | */ |
276 | 0 | size_t chunk = want > chunk_max ? chunk_max : want; |
277 | 0 | int i; |
278 | |
|
279 | 0 | if (!BUF_MEM_grow_clean(b, len + chunk)) { |
280 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB); |
281 | 0 | goto err; |
282 | 0 | } |
283 | 0 | want -= chunk; |
284 | 0 | while (chunk > 0) { |
285 | 0 | i = BIO_read(in, &(b->data[len]), (int)chunk); |
286 | 0 | if (i <= 0) { |
287 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA); |
288 | 0 | goto err; |
289 | 0 | } |
290 | | /* |
291 | | * This can't overflow because |len+want| didn't |
292 | | * overflow. |
293 | | */ |
294 | 0 | len += i; |
295 | 0 | chunk -= i; |
296 | 0 | } |
297 | 0 | if (chunk_max < INT_MAX / 2) |
298 | 0 | chunk_max *= 2; |
299 | 0 | } |
300 | 0 | } |
301 | 0 | if (off + slen < off) { |
302 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG); |
303 | 0 | goto err; |
304 | 0 | } |
305 | 0 | off += slen; |
306 | 0 | if (eos == 0) { |
307 | 0 | break; |
308 | 0 | } else |
309 | 0 | want = HEADER_SIZE; |
310 | 0 | } |
311 | 0 | } |
312 | | |
313 | 0 | if (off > INT_MAX) { |
314 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG); |
315 | 0 | goto err; |
316 | 0 | } |
317 | | |
318 | 0 | *pb = b; |
319 | 0 | ERR_clear_last_mark(); |
320 | 0 | return (int)off; |
321 | 0 | err: |
322 | 0 | ERR_clear_last_mark(); |
323 | 0 | BUF_MEM_free(b); |
324 | 0 | return -1; |
325 | 0 | } |