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>
16 #include "internal/asn1_int.h"
19 # ifndef OPENSSL_NO_STDIO
21 void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x)
26 if ((b = BIO_new(BIO_s_file())) == NULL) {
27 ASN1err(ASN1_F_ASN1_D2I_FP, ERR_R_BUF_LIB);
30 BIO_set_fp(b, in, BIO_NOCLOSE);
31 ret = ASN1_d2i_bio(xnew, d2i, b, x);
37 void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x)
40 const unsigned char *p;
44 len = asn1_d2i_read_bio(in, &b);
48 p = (unsigned char *)b->data;
49 ret = d2i(x, &p, len);
57 void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
60 const unsigned char *p;
64 len = asn1_d2i_read_bio(in, &b);
68 p = (const unsigned char *)b->data;
69 ret = ASN1_item_d2i(x, &p, len, it);
75 #ifndef OPENSSL_NO_STDIO
76 void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
81 if ((b = BIO_new(BIO_s_file())) == NULL) {
82 ASN1err(ASN1_F_ASN1_ITEM_D2I_FP, ERR_R_BUF_LIB);
85 BIO_set_fp(b, in, BIO_NOCLOSE);
86 ret = ASN1_item_d2i_bio(it, b, x);
93 #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
94 int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
99 size_t want = HEADER_SIZE;
104 const unsigned char *q;
106 int inf, tag, xclass;
110 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
116 if (want >= (len - off)) {
119 if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
120 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
123 i = BIO_read(in, &(b->data[len]), want);
124 if ((i < 0) && ((len - off) == 0)) {
125 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA);
130 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
136 /* else data already loaded */
138 p = (unsigned char *)&(b->data[off]);
140 inf = ASN1_get_object(&q, &slen, &tag, &xclass, len - off);
144 e = ERR_GET_REASON(ERR_peek_error());
145 if (e != ASN1_R_TOO_LONG)
148 ERR_clear_error(); /* clear error */
150 i = q - p; /* header length */
151 off += i; /* end of data */
154 /* no data body so go round again */
155 if (eos == UINT32_MAX) {
156 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG);
161 } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
162 /* eos value, so go back and read another header */
169 /* suck in slen bytes of data */
171 if (want > (len - off)) {
172 size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
175 if (want > INT_MAX /* BIO_read takes an int length */ ||
177 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
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
187 size_t chunk = want > chunk_max ? chunk_max : want;
189 if (!BUF_MEM_grow_clean(b, len + chunk)) {
190 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
195 i = BIO_read(in, &(b->data[len]), chunk);
197 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
198 ASN1_R_NOT_ENOUGH_DATA);
202 * This can't overflow because |len+want| didn't
208 if (chunk_max < INT_MAX/2)
212 if (off + slen < off) {
213 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
225 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);