1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/asn1.h>
16 
17 #include <limits.h>
18 
19 #include <openssl/bio.h>
20 #include <openssl/err.h>
21 #include <openssl/mem.h>
22 
23 
ASN1_item_d2i_bio(const ASN1_ITEM * it,BIO * in,void * x)24 void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x) {
25   uint8_t *data;
26   size_t len;
27   // Historically, this function did not impose a limit in OpenSSL and is used
28   // to read CRLs, so we leave this without an external bound.
29   if (!BIO_read_asn1(in, &data, &len, INT_MAX)) {
30     return NULL;
31   }
32   const uint8_t *ptr = data;
33   void *ret = ASN1_item_d2i(reinterpret_cast<ASN1_VALUE **>(x), &ptr, len, it);
34   OPENSSL_free(data);
35   return ret;
36 }
37 
ASN1_item_d2i_fp(const ASN1_ITEM * it,FILE * in,void * x)38 void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x) {
39   BIO *b = BIO_new_fp(in, BIO_NOCLOSE);
40   if (b == NULL) {
41     OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
42     return NULL;
43   }
44   void *ret = ASN1_item_d2i_bio(it, b, x);
45   BIO_free(b);
46   return ret;
47 }
48