various kdfs: Always reset buflen after clearing the buffer
authorTomas Mraz <tomas@openssl.org>
Tue, 30 Nov 2021 10:52:10 +0000 (11:52 +0100)
committerTomas Mraz <tomas@openssl.org>
Tue, 30 Nov 2021 10:52:10 +0000 (11:52 +0100)
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17165)

providers/implementations/kdfs/krb5kdf.c
providers/implementations/kdfs/pbkdf1.c
providers/implementations/kdfs/pbkdf2.c
providers/implementations/kdfs/pkcs12kdf.c
providers/implementations/kdfs/scrypt.c
providers/implementations/kdfs/sshkdf.c

index f8d4baa5684c405b181fb4ca271e6f396bdc481e..2c887f0eb9939407d13657c1abe6cf242bd39ed1 100644 (file)
@@ -98,6 +98,7 @@ static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,
 {
     OPENSSL_clear_free(*dst, *dst_len);
     *dst = NULL;
+    *dst_len = 0;
     return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
 }
 
index c93ff9b3ebd6d02e7a62e39f5848403f4b56df16..b9b164c4e2c1ad2db7925d8f0140c47bc5941750 100644 (file)
@@ -134,13 +134,15 @@ static int kdf_pbkdf1_set_membuf(unsigned char **buffer, size_t *buflen,
                              const OSSL_PARAM *p)
 {
     OPENSSL_clear_free(*buffer, *buflen);
+    *buffer = NULL;
+    *buflen = 0;
+
     if (p->data_size == 0) {
         if ((*buffer = OPENSSL_malloc(1)) == NULL) {
             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
             return 0;
         }
     } else if (p->data != NULL) {
-        *buffer = NULL;
         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
             return 0;
     }
index 16acf300ea31a4d59e0de151173267177aa7c0e1..b9e865f8eca38e28607e0f2a64b4fd24b22eb262 100644 (file)
@@ -126,13 +126,15 @@ static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
                              const OSSL_PARAM *p)
 {
     OPENSSL_clear_free(*buffer, *buflen);
+    *buffer = NULL;
+    *buflen = 0;
+
     if (p->data_size == 0) {
         if ((*buffer = OPENSSL_malloc(1)) == NULL) {
             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
             return 0;
         }
     } else if (p->data != NULL) {
-        *buffer = NULL;
         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
             return 0;
     }
index 7f461fe02235058303c04b9fe1b6c8301336e063..a29a618ee875ef6aed65f76b5423e815ea30ef00 100644 (file)
@@ -182,13 +182,15 @@ static int pkcs12kdf_set_membuf(unsigned char **buffer, size_t *buflen,
                              const OSSL_PARAM *p)
 {
     OPENSSL_clear_free(*buffer, *buflen);
+    *buffer = NULL;
+    *buflen = 0;
+
     if (p->data_size == 0) {
         if ((*buffer = OPENSSL_malloc(1)) == NULL) {
             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
             return 0;
         }
     } else if (p->data != NULL) {
-        *buffer = NULL;
         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
             return 0;
     }
index 2bbea0c7ccfc19fd92a5785c93658cfcae5bfb0a..a7072f785f0877ae8002bc41d0a0dfd0b24360e6 100644 (file)
@@ -108,13 +108,15 @@ static int scrypt_set_membuf(unsigned char **buffer, size_t *buflen,
                              const OSSL_PARAM *p)
 {
     OPENSSL_clear_free(*buffer, *buflen);
+    *buffer = NULL;
+    *buflen = 0;
+
     if (p->data_size == 0) {
         if ((*buffer = OPENSSL_malloc(1)) == NULL) {
             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
             return 0;
         }
     } else if (p->data != NULL) {
-        *buffer = NULL;
         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
             return 0;
     }
index 93a7a64fb5d5ce77feb9e04d081f3f8ad9370376..be23c2143d3c7602992994afae0f22207deb3fe9 100644 (file)
@@ -91,6 +91,7 @@ static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len,
 {
     OPENSSL_clear_free(*dst, *dst_len);
     *dst = NULL;
+    *dst_len = 0;
     return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
 }