2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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"
20 # ifndef OPENSSL_NO_STDIO
22 void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x)
27 if ((b = BIO_new(BIO_s_file())) == NULL) {
28 ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
31 BIO_set_fp(b, in, BIO_NOCLOSE);
32 ret = ASN1_d2i_bio(xnew, d2i, b, x);
38 void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x)
41 const unsigned char *p;
45 len = asn1_d2i_read_bio(in, &b);
49 p = (unsigned char *)b->data;
50 ret = d2i(x, &p, len);
58 void *ASN1_item_d2i_bio_ex(const ASN1_ITEM *it, BIO *in, void *x,
59 OSSL_LIB_CTX *libctx, const char *propq)
62 const unsigned char *p;
68 len = asn1_d2i_read_bio(in, &b);
72 p = (const unsigned char *)b->data;
73 ret = ASN1_item_d2i_ex(x, &p, len, it, libctx, propq);
79 void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
81 return ASN1_item_d2i_bio_ex(it, in, x, NULL, NULL);
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)
91 if ((b = BIO_new(BIO_s_file())) == NULL) {
92 ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
95 BIO_set_fp(b, in, BIO_NOCLOSE);
96 ret = ASN1_item_d2i_bio_ex(it, b, x, libctx, propq);
101 void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
103 return ASN1_item_d2i_fp_ex(it, in, x, NULL, NULL);
107 #define HEADER_SIZE 8
108 #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
109 int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
114 size_t want = HEADER_SIZE;
120 const unsigned char *q;
122 int inf, tag, xclass;
126 ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
136 if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
137 ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
140 i = BIO_read(in, &(b->data[len]), want);
141 if (i < 0 && diff == 0) {
142 ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
147 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
153 /* else data already loaded */
155 p = (unsigned char *)&(b->data[off]);
160 inf = ASN1_get_object(&q, &slen, &tag, &xclass, diff);
164 e = ERR_GET_REASON(ERR_peek_last_error());
165 if (e != ASN1_R_TOO_LONG)
169 i = q - p; /* header length */
170 off += i; /* end of data */
173 /* no data body so go round again */
174 if (eos == UINT32_MAX) {
175 ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
180 } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
181 /* eos value, so go back and read another header */
188 /* suck in slen bytes of data */
190 if (want > (len - off)) {
191 size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
194 if (want > INT_MAX /* BIO_read takes an int length */ ||
196 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
201 * Read content in chunks of increasing size
202 * so we can return an error for EOF without
203 * having to allocate the entire content length
206 size_t chunk = want > chunk_max ? chunk_max : want;
208 if (!BUF_MEM_grow_clean(b, len + chunk)) {
209 ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
214 i = BIO_read(in, &(b->data[len]), chunk);
216 ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
220 * This can't overflow because |len+want| didn't
226 if (chunk_max < INT_MAX/2)
230 if (off + slen < off) {
231 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
243 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
250 ERR_clear_last_mark();