Unify errors treating from OpenSSL functions. 55/232155/6
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Tue, 28 Apr 2020 14:33:25 +0000 (16:33 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Fri, 26 Jun 2020 15:36:20 +0000 (17:36 +0200)
Have 3 lines block almost everywhere where we call OpenSSL function
and want to handle its error code. Always ERROR_DUMP in such a case.

Also some other unification of OpenSSL returns where we don't care
about its errors (loading keys where errors are expected due to
autodetection).

Change-Id: Ie9e2f19bae099cfaddaa9c45a6de985f09b3f97b

src/crypto.c
src/digest.c
src/encrypt.c
src/key.c
src/sign.c

index 7123ea84d7610113cae55ceac2db3a27013058dc..98e941ac0a82a739cba7c079e6f6f6728db636ef 100644 (file)
@@ -331,8 +331,9 @@ API int yaca_malloc(size_t size, void **memory)
 
        *memory = OPENSSL_malloc(size);
        if (*memory == NULL) {
-               ERROR_DUMP(YACA_ERROR_OUT_OF_MEMORY);
-               return YACA_ERROR_OUT_OF_MEMORY;
+               const int ret = YACA_ERROR_OUT_OF_MEMORY;
+               ERROR_DUMP(ret);
+               return ret;
        }
 
        return YACA_ERROR_NONE;
@@ -356,8 +357,9 @@ API int yaca_realloc(size_t size, void **memory)
 
        void *tmp = OPENSSL_realloc(*memory, size);
        if (tmp == NULL) {
-               ERROR_DUMP(YACA_ERROR_OUT_OF_MEMORY);
-               return YACA_ERROR_OUT_OF_MEMORY;
+               const int ret = YACA_ERROR_OUT_OF_MEMORY;
+               ERROR_DUMP(ret);
+               return ret;
        }
 
        *memory = tmp;
index 9812d6dac9ff4aec3456ac00bdff5120d120005c..9b88442b58abc3b153e9dcc04b8aa4f01af293a9 100644 (file)
@@ -93,8 +93,11 @@ static int get_digest_output_length(const yaca_context_h ctx,
                return YACA_ERROR_INVALID_PARAMETER;
 
        int md_size = EVP_MD_CTX_size(c->md_ctx);
-       if (md_size <= 0)
-               return YACA_ERROR_INTERNAL;
+       if (md_size <= 0) {
+               const int ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
+       }
 
        *output_len = md_size;
 
@@ -130,6 +133,7 @@ int digest_get_algorithm(yaca_digest_algorithm_e algo, const EVP_MD **md)
        if (ret == YACA_ERROR_NONE && *md == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
+               return ret;
        }
 
        return ret;
index 510e6f8885685e6131dfc63297121ed5de4bbdba..a369e53d675e7489e288a3bdfb7e9ee8e9f17a72 100644 (file)
@@ -305,8 +305,9 @@ static int get_encrypt_output_length(const yaca_context_h ctx, size_t input_len,
 
        block_size = EVP_CIPHER_CTX_block_size(c->cipher_ctx);
        if (block_size <= 0) {
-               ERROR_DUMP(YACA_ERROR_INTERNAL);
-               return YACA_ERROR_INTERNAL;
+               const int ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
        }
 
        if (input_len > 0) {
@@ -504,8 +505,11 @@ static int encrypt_ctx_setup(struct yaca_encrypt_context_s *c,
        assert(key != YACA_KEY_NULL);
 
        const EVP_CIPHER *cipher = EVP_CIPHER_CTX_cipher(c->cipher_ctx);
-       if (cipher == NULL)
-               return YACA_ERROR_INTERNAL;
+       if (cipher == NULL) {
+               ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
+       }
 
        lkey = key_get_simple(key);
        assert(lkey != NULL);
@@ -699,8 +703,9 @@ static int set_encrypt_property(yaca_context_h ctx,
                        return YACA_ERROR_INVALID_PARAMETER;
 
                if (EVP_CipherUpdate(c->cipher_ctx, NULL, &len, value, value_len) != 1) {
-                       ERROR_DUMP(YACA_ERROR_INTERNAL);
-                       return YACA_ERROR_INTERNAL;
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       return ret;
                }
                c->state = ENC_CTX_AAD_UPDATED;
                break;
@@ -710,8 +715,9 @@ static int set_encrypt_property(yaca_context_h ctx,
                        return YACA_ERROR_INVALID_PARAMETER;
 
                if (EVP_CipherUpdate(c->cipher_ctx, NULL, &len, value, value_len) != 1) {
-                       ERROR_DUMP(YACA_ERROR_INTERNAL);
-                       return YACA_ERROR_INTERNAL;
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       return ret;
                }
                c->state = ENC_CTX_AAD_UPDATED;
                break;
@@ -722,8 +728,9 @@ static int set_encrypt_property(yaca_context_h ctx,
                        return YACA_ERROR_INVALID_PARAMETER;
 
                if (EVP_CIPHER_CTX_ctrl(c->cipher_ctx, EVP_CTRL_GCM_SET_TAG, value_len, (void*)value) != 1) {
-                       ERROR_DUMP(YACA_ERROR_INTERNAL);
-                       return YACA_ERROR_INTERNAL;
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       return ret;
                }
                c->state = ENC_CTX_TAG_SET;
                break;
@@ -772,8 +779,9 @@ static int set_encrypt_property(yaca_context_h ctx,
 
                int padding = *(yaca_padding_e*)value == YACA_PADDING_NONE ? 0 : 1;
                if (EVP_CIPHER_CTX_set_padding(c->cipher_ctx, padding) != 1) {
-                       ERROR_DUMP(YACA_ERROR_INTERNAL);
-                       return YACA_ERROR_INTERNAL;
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       return ret;
                }
                if (c->backup_ctx != NULL)
                        c->backup_ctx->padding = padding;
@@ -825,9 +833,9 @@ static int get_encrypt_property(const yaca_context_h ctx, yaca_property_e proper
                                                                EVP_CTRL_GCM_GET_TAG,
                                                                c->tag_len,
                                                                tag) != 1) {
-                       yaca_free(tag);
-                       ERROR_DUMP(YACA_ERROR_INTERNAL);
-                       return YACA_ERROR_INTERNAL;
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       goto err;
                }
                *value = tag;
                *value_len = c->tag_len;
@@ -849,9 +857,9 @@ static int get_encrypt_property(const yaca_context_h ctx, yaca_property_e proper
                                                                EVP_CTRL_CCM_GET_TAG,
                                                                c->tag_len,
                                                                tag) != 1) {
-                       yaca_free(tag);
-                       ERROR_DUMP(YACA_ERROR_INTERNAL);
-                       return YACA_ERROR_INTERNAL;
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       goto err;
                }
                *value = tag;
                *value_len = c->tag_len;
@@ -862,6 +870,10 @@ static int get_encrypt_property(const yaca_context_h ctx, yaca_property_e proper
        }
 
        return YACA_ERROR_NONE;
+
+err:
+       yaca_free(tag);
+       return ret;
 }
 
 static int check_key_bit_length_for_algo(yaca_encrypt_algorithm_e algo, size_t key_bit_len)
@@ -938,6 +950,7 @@ int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
        if (ret == YACA_ERROR_NONE && *cipher == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
+               return ret;
        }
 
        return ret;
@@ -1070,12 +1083,12 @@ int encrypt_update(yaca_context_h ctx,
                         * It does not necessarily indicate a more serious error.
                         * There is no call to EVP_CipherFinal.
                         */
-                       ret = YACA_ERROR_INVALID_PARAMETER;
+                       return YACA_ERROR_INVALID_PARAMETER;
                } else {
                        ret = YACA_ERROR_INTERNAL;
                        ERROR_DUMP(ret);
+                       return ret;
                }
-               return ret;
        }
 
        *output_len = loutput_len;
@@ -1134,8 +1147,8 @@ API int yaca_encrypt_get_iv_bit_length(yaca_encrypt_algorithm_e algo,
                                                                           size_t key_bit_len,
                                                                           size_t *iv_bit_len)
 {
-       const EVP_CIPHER *cipher;
        int ret;
+       const EVP_CIPHER *cipher;
 
        if (iv_bit_len == NULL || key_bit_len % 8 != 0)
                return YACA_ERROR_INVALID_PARAMETER;
@@ -1146,8 +1159,9 @@ API int yaca_encrypt_get_iv_bit_length(yaca_encrypt_algorithm_e algo,
 
        ret = EVP_CIPHER_iv_length(cipher);
        if (ret < 0) {
-               ERROR_DUMP(YACA_ERROR_INTERNAL);
-               return YACA_ERROR_INTERNAL;
+               ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
        }
 
        *iv_bit_len = ret * 8;
index 15bc937812605ebdc24e2e315583f38ac01e61c0..75e3a57d29ad4cfed5f954d8ca57919220b820bf 100644 (file)
--- a/src/key.c
+++ b/src/key.c
@@ -320,18 +320,18 @@ static EVP_PKEY *d2i_DSAparams_bio_helper(BIO *src)
 
        dsa = d2i_DSAparams_bio(src, NULL);
        if (dsa == NULL)
-               return NULL;
+               goto err;
 
        pkey = EVP_PKEY_new();
        if (pkey == NULL)
-               goto exit;
+               goto err;
 
        if (EVP_PKEY_assign_DSA(pkey, dsa) != 1)
-               goto exit;
+               goto err;
 
        return pkey;
 
-exit:
+err:
        EVP_PKEY_free(pkey);
        DSA_free(dsa);
        return NULL;
@@ -346,18 +346,18 @@ static EVP_PKEY *d2i_DHparams_bio_helper(BIO *src)
 
        dh = d2i_DHparams_bio(src, NULL);
        if (dh == NULL)
-               return NULL;
+               goto err;
 
        pkey = EVP_PKEY_new();
        if (pkey == NULL)
-               goto exit;
+               goto err;
 
        if (EVP_PKEY_assign_DH(pkey, dh) != 1)
-               goto exit;
+               goto err;
 
        return pkey;
 
-exit:
+err:
        EVP_PKEY_free(pkey);
        DH_free(dh);
        return NULL;
@@ -373,28 +373,28 @@ static EVP_PKEY *d2i_ECPKParameters_bio_helper(BIO *src)
 
        ecg = d2i_ECPKParameters_bio(src, NULL);
        if (ecg == NULL)
-               return NULL;
+               goto err;
 
        eck = EC_KEY_new();
        if (eck == NULL)
-               goto exit;
+               goto err;
 
        if (EC_KEY_set_group(eck, ecg) != 1)
-               goto exit;
+               goto err;
 
        EC_GROUP_free(ecg);
        ecg = NULL;
 
        pkey = EVP_PKEY_new();
        if (pkey == NULL)
-               goto exit;
+               goto err;
 
        if (EVP_PKEY_assign_EC_KEY(pkey, eck) != 1)
-               goto exit;
+               goto err;
 
        return pkey;
 
-exit:
+err:
        EVP_PKEY_free(pkey);
        EC_KEY_free(eck);
        EC_GROUP_free(ecg);
@@ -437,8 +437,9 @@ static int import_evp(yaca_key_h *key,
 
        src = BIO_new_mem_buf(data, data_len);
        if (src == NULL) {
-               ERROR_DUMP(YACA_ERROR_INTERNAL);
-               return YACA_ERROR_INTERNAL;
+               ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
        }
 
        /* Possible PEM */
index 66fd5b2c700cdd70fad345bf106f72a91f9e83a7..e276af67d49d611592bb25899fa97685a5eadc1f 100644 (file)
@@ -84,6 +84,7 @@ static int get_sign_output_length(const yaca_context_h ctx,
 {
        assert(output_len != NULL);
 
+       int ret;
        EVP_PKEY_CTX *pctx;
        struct yaca_sign_context_s *c = get_sign_context(ctx);
        assert(c != NULL);
@@ -93,19 +94,24 @@ static int get_sign_output_length(const yaca_context_h ctx,
                return YACA_ERROR_INVALID_PARAMETER;
 
        pctx = EVP_MD_CTX_pkey_ctx(c->md_ctx);
-       if (pctx == NULL)
-               return YACA_ERROR_INTERNAL;
+       if (pctx == NULL) {
+               ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
+       }
 
        EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
        if (pkey == NULL) {
-               ERROR_DUMP(YACA_ERROR_INTERNAL);
-               return YACA_ERROR_INTERNAL;
+               ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
        }
 
        int len = EVP_PKEY_size(pkey);
        if (len <= 0) {
-               ERROR_DUMP(YACA_ERROR_INTERNAL);
-               return YACA_ERROR_INTERNAL;
+               ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
        }
 
        *output_len = len;
@@ -139,8 +145,11 @@ int set_sign_property(yaca_context_h ctx,
                return YACA_ERROR_INVALID_PARAMETER;
 
        pctx = EVP_MD_CTX_pkey_ctx(c->md_ctx);
-       if (pctx == NULL)
-               return YACA_ERROR_INTERNAL;
+       if (pctx == NULL) {
+               ret = YACA_ERROR_INTERNAL;
+               ERROR_DUMP(ret);
+               return ret;
+       }
 
        /* this function only supports padding */
        if (property != YACA_PROPERTY_PADDING || value_len != sizeof(yaca_padding_e))