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