f6a62c4
commit 515c728dbaa92211d2eafb0041ab9fcd258fdc41
f6a62c4
Author: Bernd Edlinger <bernd.edlinger@hotmail.de>
f6a62c4
Date:   Mon Sep 9 19:12:25 2019 +0200
a71f5ae
f6a62c4
    Fix potential memory leaks with BN_to_ASN1_INTEGER
f6a62c4
    
f6a62c4
    Reviewed-by: Paul Dale <paul.dale@oracle.com>
f6a62c4
    Reviewed-by: Matt Caswell <matt@openssl.org>
f6a62c4
    (Merged from https://github.com/openssl/openssl/pull/9833)
f6a62c4
    
f6a62c4
    (cherry picked from commit f28bc7d386b25fb75625d0c62c6b2e6d21de0d09)
a71f5ae
f6a62c4
diff --git a/crypto/ec/ec_asn1.c b/crypto/ec/ec_asn1.c
f6a62c4
index 1ce1181fc1..7cbf8de981 100644
f6a62c4
--- a/crypto/ec/ec_asn1.c
f6a62c4
+++ b/crypto/ec/ec_asn1.c
f6a62c4
@@ -446,6 +446,7 @@ ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
f6a62c4
     unsigned char *buffer = NULL;
f6a62c4
     const EC_POINT *point = NULL;
f6a62c4
     point_conversion_form_t form;
f6a62c4
+    ASN1_INTEGER *orig;
f6a62c4
 
f6a62c4
     if (params == NULL) {
f6a62c4
         if ((ret = ECPARAMETERS_new()) == NULL) {
f6a62c4
@@ -496,8 +497,9 @@ ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
f6a62c4
         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
f6a62c4
         goto err;
a71f5ae
     }
f6a62c4
-    ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
f6a62c4
+    ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order);
f6a62c4
     if (ret->order == NULL) {
f6a62c4
+        ret->order = orig;
f6a62c4
         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
f6a62c4
         goto err;
f6a62c4
     }
f6a62c4
@@ -505,8 +507,9 @@ ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
f6a62c4
     /* set the cofactor (optional) */
f6a62c4
     tmp = EC_GROUP_get0_cofactor(group);
f6a62c4
     if (tmp != NULL) {
f6a62c4
-        ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
f6a62c4
+        ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor);
f6a62c4
         if (ret->cofactor == NULL) {
f6a62c4
+            ret->cofactor = orig;
f6a62c4
             ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
f6a62c4
             goto err;
f6a62c4
         }
f6a62c4
diff --git a/crypto/x509v3/v3_asid.c b/crypto/x509v3/v3_asid.c
f6a62c4
index 089f2ae29f..ef2d64826f 100644
f6a62c4
--- a/crypto/x509v3/v3_asid.c
f6a62c4
+++ b/crypto/x509v3/v3_asid.c
f6a62c4
@@ -256,6 +256,7 @@ static int extract_min_max(ASIdOrRange *aor,
f6a62c4
 static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
a71f5ae
 {
f6a62c4
     ASN1_INTEGER *a_max_plus_one = NULL;
f6a62c4
+    ASN1_INTEGER *orig;
f6a62c4
     BIGNUM *bn = NULL;
f6a62c4
     int i, ret = 0;
f6a62c4
 
f6a62c4
@@ -298,9 +299,15 @@ static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
f6a62c4
          */
f6a62c4
         if ((bn == NULL && (bn = BN_new()) == NULL) ||
f6a62c4
             ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
f6a62c4
-            !BN_add_word(bn, 1) ||
f6a62c4
-            (a_max_plus_one =
f6a62c4
-             BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
f6a62c4
+            !BN_add_word(bn, 1)) {
f6a62c4
+            X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
f6a62c4
+                      ERR_R_MALLOC_FAILURE);
f6a62c4
+            goto done;
f6a62c4
+        }
f6a62c4
+
f6a62c4
+        if ((a_max_plus_one =
f6a62c4
+                BN_to_ASN1_INTEGER(bn, orig = a_max_plus_one)) == NULL) {
f6a62c4
+            a_max_plus_one = orig;
f6a62c4
             X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
f6a62c4
                       ERR_R_MALLOC_FAILURE);
f6a62c4
             goto done;
f6a62c4
@@ -351,6 +358,7 @@ int X509v3_asid_is_canonical(ASIdentifiers *asid)
f6a62c4
 static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
f6a62c4
 {
f6a62c4
     ASN1_INTEGER *a_max_plus_one = NULL;
f6a62c4
+    ASN1_INTEGER *orig;
f6a62c4
     BIGNUM *bn = NULL;
f6a62c4
     int i, ret = 0;
f6a62c4
 
f6a62c4
@@ -416,9 +424,15 @@ static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
f6a62c4
          */
f6a62c4
         if ((bn == NULL && (bn = BN_new()) == NULL) ||
f6a62c4
             ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
f6a62c4
-            !BN_add_word(bn, 1) ||
f6a62c4
-            (a_max_plus_one =
f6a62c4
-             BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
f6a62c4
+            !BN_add_word(bn, 1)) {
f6a62c4
+            X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
f6a62c4
+                      ERR_R_MALLOC_FAILURE);
f6a62c4
+            goto done;
f6a62c4
+        }
f6a62c4
+
f6a62c4
+        if ((a_max_plus_one =
f6a62c4
+                 BN_to_ASN1_INTEGER(bn, orig = a_max_plus_one)) == NULL) {
f6a62c4
+            a_max_plus_one = orig;
f6a62c4
             X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
f6a62c4
                       ERR_R_MALLOC_FAILURE);
f6a62c4
             goto done;
f6a62c4
commit 86ed78676c660b553696cc10c682962522dfeb6c
f6a62c4
Author: Tomas Mraz <tmraz@fedoraproject.org>
f6a62c4
Date:   Thu Sep 12 12:27:36 2019 +0200
a71f5ae
f6a62c4
    BIO_f_zlib: Properly handle BIO_CTRL_PENDING and BIO_CTRL_WPENDING calls.
f6a62c4
    
f6a62c4
    There can be data to write in output buffer and data to read that were
f6a62c4
    not yet read in the input stream.
f6a62c4
    
f6a62c4
    Fixes #9866
f6a62c4
    
f6a62c4
    Reviewed-by: Richard Levitte <levitte@openssl.org>
f6a62c4
    (Merged from https://github.com/openssl/openssl/pull/9877)
f6a62c4
    
f6a62c4
    (cherry picked from commit 6beb8b39ba8e4cb005c1fcd2586ba19e17f04b95)
f6a62c4
f6a62c4
diff --git a/crypto/comp/c_zlib.c b/crypto/comp/c_zlib.c
f6a62c4
index d688deee5f..7c1be358fd 100644
f6a62c4
--- a/crypto/comp/c_zlib.c
f6a62c4
+++ b/crypto/comp/c_zlib.c
f6a62c4
@@ -598,6 +598,28 @@ static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
f6a62c4
         BIO_copy_next_retry(b);
f6a62c4
         break;
f6a62c4
 
f6a62c4
+    case BIO_CTRL_WPENDING:
f6a62c4
+        if (ctx->obuf == NULL)
f6a62c4
+            return 0;
f6a62c4
+
f6a62c4
+        if (ctx->odone) {
f6a62c4
+            ret = ctx->ocount;
f6a62c4
+        } else {
f6a62c4
+            ret = ctx->ocount;
f6a62c4
+            if (ret == 0)
f6a62c4
+                /* Unknown amount pending but we are not finished */
f6a62c4
+                ret = 1;
f6a62c4
+        }
f6a62c4
+        if (ret == 0)
f6a62c4
+            ret = BIO_ctrl(next, cmd, num, ptr);
f6a62c4
+        break;
8419f76
+
f6a62c4
+    case BIO_CTRL_PENDING:
f6a62c4
+        ret = ctx->zin.avail_in;
f6a62c4
+        if (ret == 0)
f6a62c4
+            ret = BIO_ctrl(next, cmd, num, ptr);
f6a62c4
+        break;
8419f76
+
f6a62c4
     default:
f6a62c4
         ret = BIO_ctrl(next, cmd, num, ptr);
f6a62c4
         break;