Merge cherrypicks of [2331043, 2330986, 2331024, 2331025, 2330859, 2330970, 2331044, 2330893, 2330950, 2330951, 2330940, 2331045, 2330954, 2330799, 2330987, 2331004, 2331061, 2331005, 2331047, 2330955, 2331030, 2331031, 2331101, 2330972, 2330956, 2331032, 2331006, 2330894, 2331063, 2331064, 2330895, 2331048, 2331102, 2331035, 2331007, 2331083, 2331103, 2331104, 2330897, 2331084, 2331067, 2331009, 2331010, 2330898, 2330989, 2331105, 2330899, 2331011, 2331069, 2330990, 2331121, 2331122, 2331086] into nyc-mr2-security-a-release

Change-Id: Ib82a9ac39a8c29ddc826405a0c25c4604eff2fa1
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)