summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaiki Ueno <ueno@gnu.org>2020-06-05 16:38:35 +0000
committerDaiki Ueno <ueno@gnu.org>2020-06-05 16:38:35 +0000
commit16a9a2889625dacc85eff9c6c05b2c5c0777e671 (patch)
tree1453034b726078333d4ad17c70ce5820989d3a46
parent9ef1decb66a25e1e150eb06b2a96e6c1577b4375 (diff)
parentd86e7ab5a6b4723a87298e4c56b0c0edc9391ab4 (diff)
downloadgnutls-16a9a2889625dacc85eff9c6c05b2c5c0777e671.tar.gz
Merge branch 'tmp-aead-init-leak' into 'master'
gnutls_aead_cipher_init: fix potential memleak Closes #1010 See merge request gnutls/gnutls!1274
-rw-r--r--lib/crypto-api.c33
1 files changed, 25 insertions, 8 deletions
diff --git a/lib/crypto-api.c b/lib/crypto-api.c
index 45be64ed1f..a815379e87 100644
--- a/lib/crypto-api.c
+++ b/lib/crypto-api.c
@@ -70,20 +70,30 @@ gnutls_cipher_init(gnutls_cipher_hd_t * handle,
if (e == NULL || (e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD))
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
- *handle = gnutls_calloc(1, sizeof(api_cipher_hd_st));
- if (*handle == NULL) {
+ h = gnutls_calloc(1, sizeof(api_cipher_hd_st));
+ if (h == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- h = *handle;
ret =
_gnutls_cipher_init(&h->ctx_enc, e, key,
iv, 1);
+ if (ret < 0) {
+ gnutls_free(h);
+ return ret;
+ }
- if (ret >= 0 && _gnutls_cipher_type(e) == CIPHER_BLOCK)
+ if (_gnutls_cipher_type(e) == CIPHER_BLOCK) {
ret =
_gnutls_cipher_init(&h->ctx_dec, e, key, iv, 0);
+ if (ret < 0) {
+ gnutls_free(h);
+ return ret;
+ }
+ }
+
+ *handle = h;
return ret;
}
@@ -755,6 +765,7 @@ int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
{
api_aead_cipher_hd_st *h;
const cipher_entry_st *e;
+ int ret;
if (is_cipher_algo_forbidden(cipher))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
@@ -763,15 +774,21 @@ int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
if (e == NULL || e->type != CIPHER_AEAD)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
- *handle = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
- if (*handle == NULL) {
+ h = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
+ if (h == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- h = *handle;
+ ret = _gnutls_aead_cipher_init(h, cipher, key);
+ if (ret < 0) {
+ gnutls_free(h);
+ return ret;
+ }
+
+ *handle = h;
- return _gnutls_aead_cipher_init(h, cipher, key);
+ return ret;
}
/**