Merge cherrypicks of [2338295, 2338197, 2338407, 2338385, 2338425, 2338465, 2338447, 2338426, 2338386, 2338387, 2338466, 2338368, 2338296, 2338198, 2338450, 2338470, 2338429, 2338390, 2338430, 2338315, 2338452, 2338453, 2338431, 2338297, 2338354, 2338200, 2338391, 2338392, 2338482, 2338357, 2338411, 2338394, 2338318, 2338370, 2338434, 2338472, 2338473, 2338395, 2338299, 2338412, 2338413, 2338454, 2338396, 2338474, 2338397, 2338360, 2338455] into nyc-mr2-security-b-release

Change-Id: Ie989c9e8d977c1e4e668cbc07b7839171f2dc9b9
diff --git a/src/crypto/asn1/a_d2i_fp.c b/src/crypto/asn1/a_d2i_fp.c
index 97ec75b..af03bc0 100644
--- a/src/crypto/asn1/a_d2i_fp.c
+++ b/src/crypto/asn1/a_d2i_fp.c
@@ -140,6 +140,7 @@
 #endif
 
 #define HEADER_SIZE   8
+#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
 static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
 	{
 	BUF_MEM *b;
@@ -231,6 +232,7 @@
 			want=c.slen;
 			if (want > (len-off))
 				{
+                                size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
 				want-=(len-off);
 				if (want > INT_MAX /* BIO_read takes an int length */ ||
 					len+want < len)
@@ -238,23 +240,37 @@
 						OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
 						goto err;
 						}
-				if (!BUF_MEM_grow_clean(b,len+want))
-					{
-					OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
-					goto err;
-					}
 				while (want > 0)
 					{
-					i=BIO_read(in,&(b->data[len]),want);
-					if (i <= 0)
-						{
-						OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
-						goto err;
-						}
-					/* This can't overflow because
-					 * |len+want| didn't overflow. */
-					len+=i;
-					want-=i;
+
+                                        /*
+                                         * Read content in chunks of increasing size
+                                         * so we can return an error for EOF without
+                                         * having to allocate the entire content length
+                                         * in one go.
+                                         */
+                                        size_t chunk = want > chunk_max ? chunk_max : want;
+
+                                        if (!BUF_MEM_grow_clean(b, len + chunk)) {
+                                          OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
+                                          goto err;
+                                        }
+                                        want -= chunk;
+                                        while (chunk > 0) {
+                                          i = BIO_read(in, &(b->data[len]), chunk);
+                                          if (i <= 0) {
+                                            OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
+                                            goto err;
+                                          }
+                                          /*
+                                           * This can't overflow because |len+want| didn't
+                                           * overflow.
+                                           */
+                                          len += i;
+                                          chunk -= i;
+                                        }
+                                        if (chunk_max < INT_MAX/2)
+                                          chunk_max *= 2;
 					}
 				}
 			if (off + c.slen < off)