2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include "internal/numbers.h"
14 #include <openssl/buffer.h>
15 #include <openssl/asn1.h>
17 static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
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 ASN1err(ASN1_F_ASN1_D2I_FP, 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(const ASN1_ITEM *it, BIO *in, void *x)
61 const unsigned char *p;
65 len = asn1_d2i_read_bio(in, &b);
69 p = (const unsigned char *)b->data;
70 ret = ASN1_item_d2i(x, &p, len, it);
76 #ifndef OPENSSL_NO_STDIO
77 void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
82 if ((b = BIO_new(BIO_s_file())) == NULL) {
83 ASN1err(ASN1_F_ASN1_ITEM_D2I_FP, ERR_R_BUF_LIB);
86 BIO_set_fp(b, in, BIO_NOCLOSE);
87 ret = ASN1_item_d2i_bio(it, b, x);
94 #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
95 static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
100 size_t want = HEADER_SIZE;
105 const unsigned char *q;
107 int inf, tag, xclass;
111 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
117 if (want >= (len - off)) {
120 if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
121 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
124 i = BIO_read(in, &(b->data[len]), want);
125 if ((i < 0) && ((len - off) == 0)) {
126 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA);
131 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
137 /* else data already loaded */
139 p = (unsigned char *)&(b->data[off]);
141 inf = ASN1_get_object(&q, &slen, &tag, &xclass, len - off);
145 e = ERR_GET_REASON(ERR_peek_error());
146 if (e != ASN1_R_TOO_LONG)
149 ERR_clear_error(); /* clear error */
151 i = q - p; /* header length */
152 off += i; /* end of data */
155 /* no data body so go round again */
156 if (eos == UINT32_MAX) {
157 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG);
162 } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
163 /* eos value, so go back and read another header */
170 /* suck in slen bytes of data */
172 if (want > (len - off)) {
173 size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
176 if (want > INT_MAX /* BIO_read takes an int length */ ||
178 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
183 * Read content in chunks of increasing size
184 * so we can return an error for EOF without
185 * having to allocate the entire content length
188 size_t chunk = want > chunk_max ? chunk_max : want;
190 if (!BUF_MEM_grow_clean(b, len + chunk)) {
191 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
196 i = BIO_read(in, &(b->data[len]), chunk);
198 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
199 ASN1_R_NOT_ENOUGH_DATA);
203 * This can't overflow because |len+want| didn't
209 if (chunk_max < INT_MAX/2)
213 if (off + slen < off) {
214 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
226 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);