diff options
author | Kyungwook Tak <k.tak@samsung.com> | 2016-11-28 20:25:03 +0900 |
---|---|---|
committer | kyungwook tak <k.tak@samsung.com> | 2016-11-28 03:37:08 -0800 |
commit | 0780d62047e7d7472c65c791866e7a5fa9be7863 (patch) | |
tree | 1aca5579ddd727823b738ae1f77df1d7d756ca38 | |
parent | 8ccec6785ee30fb7c9e06a3421f5b460364b2968 (diff) | |
download | libwebappenc-0780d62047e7d7472c65c791866e7a5fa9be7863.tar.gz libwebappenc-0780d62047e7d7472c65c791866e7a5fa9be7863.tar.bz2 libwebappenc-0780d62047e7d7472c65c791866e7a5fa9be7863.zip |
Fix svace defects
1) Missing returned value checking.
2) variable misused (start -> end).
3) Dead code exist in preloaded app dek loading while loop.
Make subroutine to simplify loop codes/resource managed.
Change-Id: Ic775e336e6480dfb56539e382edf292f2101ec8b
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
-rw-r--r-- | srcs/key_handler.c | 147 |
1 files changed, 70 insertions, 77 deletions
diff --git a/srcs/key_handler.c b/srcs/key_handler.c index cf3de1d..4607fdd 100644 --- a/srcs/key_handler.c +++ b/srcs/key_handler.c @@ -211,7 +211,7 @@ static int _extract_pkg_id_from_file_name(const char *file_name, char *pkg_id) start = start + strlen(APP_DEK_FILE_PFX) + 1; char *end = strstr(file_name, ".adek"); - if (start == NULL) { + if (end == NULL) { WAE_SLOGE("WAE: Fail to extract pkgid from APP_DEK file. file_name=%s", file_name); return WAE_ERROR_FILE; } @@ -236,6 +236,60 @@ int _write_encrypted_app_dek_to_file(const char *pkg_id, const raw_buffer_s *enc return _write_to_file(path, encrypted); } +int _load_preloaded_app_dek( + const raw_buffer_s *prikey, const char *filepath, const char *pkg_id) +{ + raw_buffer_s *encrypted_dek = NULL; + raw_buffer_s *dek = NULL; + raw_buffer_s *iv = NULL; + crypto_element_s *ce = NULL; + + int ret = _read_from_file(filepath, &encrypted_dek); + if (ret != WAE_ERROR_NONE) { + WAE_SLOGW("Failed to read file. It will be ignored. file=%s", filepath); + return ret; + } + + ret = decrypt_app_dek(prikey, APP_DEK_KEK_PRIKEY_PASSWORD, encrypted_dek, &dek); + if (ret != WAE_ERROR_NONE) { + WAE_SLOGW("Failed to decrypt dek. It will be ignored. file=%s", filepath); + goto finish; + } + + iv = buffer_create(IV_LEN); + if (iv == NULL) { + ret = WAE_ERROR_MEMORY; + goto finish; + } + + memcpy(iv->buf, AES_CBC_IV, iv->size); + + ce = crypto_element_create(dek, iv); + if (ce == NULL) { + ret = WAE_ERROR_MEMORY; + goto finish; + } + + ret = save_to_key_manager(pkg_id, pkg_id, WAE_PRELOADED_APP, ce); + if (ret == WAE_ERROR_KEY_EXISTS) { + WAE_SLOGI("Key Manager already has dek. It will be ignored. file=%s", filepath); + } else if (ret != WAE_ERROR_NONE) { + WAE_SLOGW("Fail to add APP DEK to key-manager. file=%s", filepath); + } + +finish: + buffer_destroy(encrypted_dek); + + if (ce == NULL) { + buffer_destroy(dek); + buffer_destroy(iv); + } else { + crypto_element_destroy(ce); + } + + return ret; +} + int get_app_ce(uid_t uid, const char *pkg_id, wae_app_type_e app_type, bool create_for_migrated_app, const crypto_element_s **pce) { @@ -438,7 +492,7 @@ int create_preloaded_app_ce(const char *pkg_id, const crypto_element_s **pce) } // store APP_DEK in cache - _add_app_ce_to_cache(pkg_id, ce); + ret = _add_app_ce_to_cache(pkg_id, ce); if (ret != WAE_ERROR_NONE) { WAE_SLOGE("Failed to add ce to cache for pkg_id(%s) ret(%d)", pkg_id, ret); goto error; @@ -479,15 +533,7 @@ int load_preloaded_app_deks(bool reload) int ret = WAE_ERROR_NONE; char pkg_id[MAX_PKGID_LEN] = {0, }; - char file_path_buff[MAX_PATH_LEN]; - raw_buffer_s *encrypted_dek = NULL; - raw_buffer_s *dek = NULL; - raw_buffer_s *iv = NULL; - raw_buffer_s *prikey = NULL; - crypto_element_s *ce = NULL; - - int error_during_loading = 0; if (!reload) { // check if all deks were already loaded into key-manager. @@ -497,6 +543,7 @@ int load_preloaded_app_deks(bool reload) return ret; } + raw_buffer_s *prikey = NULL; ret = _get_app_dek_kek(&prikey); if (ret != WAE_ERROR_NONE) { @@ -508,6 +555,7 @@ int load_preloaded_app_deks(bool reload) if (dir == NULL) { WAE_SLOGE("Fail to open dir. dir=%s", _get_dek_store_path()); + buffer_destroy(prikey); return WAE_ERROR_FILE; } @@ -515,11 +563,9 @@ int load_preloaded_app_deks(bool reload) struct dirent *result = NULL; while (true) { - int error = readdir_r(dir, &entry, &result); - - if (error != 0) { + if (readdir_r(dir, &entry, &result) != 0) { ret = WAE_ERROR_FILE; - goto error; + break; } // readdir_r returns NULL in *result if the end @@ -537,7 +583,7 @@ int load_preloaded_app_deks(bool reload) if (ret < 0) { WAE_SLOGE("Failed to make file path by snprintf."); ret = WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */ - goto error; + break; } ret = _extract_pkg_id_from_file_name(entry.d_name, pkg_id); @@ -548,75 +594,22 @@ int load_preloaded_app_deks(bool reload) continue; } - ret = _read_from_file(file_path_buff, &encrypted_dek); - - if (ret != WAE_ERROR_NONE || encrypted_dek == NULL) { - ++error_during_loading; - WAE_SLOGW("Failed to read file. It will be ignored. file=%s", file_path_buff); - continue; - } - - ret = decrypt_app_dek(prikey, APP_DEK_KEK_PRIKEY_PASSWORD, encrypted_dek, &dek); - - buffer_destroy(encrypted_dek); - encrypted_dek = NULL; - - if (ret != WAE_ERROR_NONE || dek == NULL) { - ++error_during_loading; - WAE_SLOGW("Failed to decrypt dek. It will be ignored. file=%s", - file_path_buff); - continue; - } - iv = buffer_create(IV_LEN); - if (iv == NULL) { - ++error_during_loading; - buffer_destroy(dek); - dek = NULL; - continue; - } - - memcpy(iv->buf, AES_CBC_IV, iv->size); - - ce = crypto_element_create(dek, iv); - if (ce == NULL) { - ++error_during_loading; - buffer_destroy(iv); - iv = NULL; - buffer_destroy(dek); - dek = NULL; - continue; - } - - ret = save_to_key_manager(pkg_id, pkg_id, WAE_PRELOADED_APP, ce); - - if (ret == WAE_ERROR_KEY_EXISTS) { - WAE_SLOGI("Key Manager already has dek. It will be ignored. file=%s", - file_path_buff); - } else if (ret != WAE_ERROR_NONE) { - ++error_during_loading; - WAE_SLOGW("Fail to add APP DEK to key-manager. file=%s", file_path_buff); - } - - crypto_element_destroy(ce); - ce = NULL; - } - - ret = set_app_deks_loaded_to_key_manager(); - -error: - if (ret != WAE_ERROR_NONE) { - if (ce) { - crypto_element_destroy(ce); + ret = _load_preloaded_app_dek(prikey, file_path_buff, pkg_id); + if (ret != WAE_ERROR_NONE && ret != WAE_ERROR_KEY_EXISTS) { + WAE_SLOGW("Failed to load app dek(%s) ret(%d)", file_path_buff, ret); } else { - buffer_destroy(dek); - buffer_destroy(iv); + WAE_SLOGI("Successfully load app dek(%s)", file_path_buff); + ret = WAE_ERROR_NONE; } } buffer_destroy(prikey); closedir(dir); - return ret; + if (ret != WAE_ERROR_NONE) + return ret; + else + return set_app_deks_loaded_to_key_manager(); } int remove_app_ce(uid_t uid, const char *pkg_id, wae_app_type_e app_type) |