From: Krzysztof Dynowski Date: Wed, 14 Feb 2018 13:07:58 +0000 (+0100) Subject: Embedded path configuration X-Git-Tag: submit/tizen/20180412.092951~35 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5da48e810589e93c7d7487e567af46b6ab93470b;p=platform%2Fcore%2Fsecurity%2Ftef-simulator.git Embedded path configuration Change-Id: I7ae530892a878a5009cab75e0610056c42f4ce59 --- diff --git a/include/include/config.h b/include/include/config.h index 97abb4b..e10eb5c 100644 --- a/include/include/config.h +++ b/include/include/config.h @@ -33,6 +33,8 @@ #endif #endif +#define TEE_EMBEDDED_TASTORE_ROOT "/usr/apps/tee/" + #ifndef TEE_EXTRACT_ROOT #define TEE_EXTRACT_ROOT "/opt/usr/apps/ta_sdk/extract/" #endif diff --git a/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp b/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp index c609831..1a37cfb 100644 --- a/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp +++ b/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp @@ -36,6 +36,8 @@ #include #include #include + +namespace fs = boost::filesystem; /*----------------------------------------------------------------------------- * Globals *-----------------------------------------------------------------------------*/ @@ -162,24 +164,38 @@ TABinaryManager* TABinaryManager::getInstance() { bool TABinaryManager::initTA(const string &uuid) { LOGD(SIM_DAEMON, "Entry"); - pthread_rwlock_wrlock(&binaryMapLock); + string tapath; + const char * paths[] = { TEE_TASTORE_ROOT, TEE_EMBEDDED_TASTORE_ROOT, NULL }; + + for (int i = 0; paths[i] != NULL; ++i) { + if (*paths[i] == '\0') continue; // ignore empty paths + string path_to_file = paths[i] + uuid; + boost::system::error_code ec; + if (fs::exists(path_to_file, ec)) { + tapath = paths[i]; + break; + } + } + + if (tapath.empty()) { + LOGE(SIM_DAEMON, "Cannot find TA: %s", uuid.c_str()); + return false; + } + StructBinaryInfo info; bool res = false; - if (boost::filesystem::exists(TEE_TASTORE_ROOT + uuid)) { - pthread_mutex_lock(&taLock); - try { - if (unpackBinary(uuid, info)) { - binaryMap[uuid] = info; - res = true; - } - } catch (...) { - res = false; + pthread_rwlock_wrlock(&binaryMapLock); + pthread_mutex_lock(&taLock); + try { + if (unpackBinary(uuid, tapath, info)) { + binaryMap[uuid] = info; + res = true; } - pthread_mutex_unlock(&taLock); - } else { - LOGD(SIM_DAEMON, "TA not exist %s", (TEE_TASTORE_ROOT + uuid).c_str()); + } catch (...) { + res = false; } + pthread_mutex_unlock(&taLock); pthread_rwlock_unlock(&binaryMapLock); return res; } @@ -208,7 +224,7 @@ void TABinaryManager::decryptImage(StructBinaryInfo& info) { myfile.close(); } - boost::filesystem::path decName = info.imagePath + "_dec"; + fs::path decName = info.imagePath + "_dec"; // hash of Keydata is not required. string dec_command = "openssl enc " + cipher + " -d -nopad -nosalt -K " + secret + " -in " + info.imagePath + " -out " + decName.string() + @@ -219,20 +235,20 @@ void TABinaryManager::decryptImage(StructBinaryInfo& info) { } boost::system::error_code ec; - boost::filesystem::remove(boost::filesystem::path(info.imagePath), ec); + fs::remove(fs::path(info.imagePath), ec); if (ec) { LOGE(SIM_DAEMON, "Post decryption failed: unlink %s : %s", info.imagePath.c_str(), ec.message()); } - boost::filesystem::rename(decName, boost::filesystem::path(info.imagePath), ec); + fs::rename(decName, fs::path(info.imagePath), ec); if (ec) { LOGE(SIM_DAEMON, "Post decryption failed: rename %s -> %s : %s", decName.string().c_str(), info.imagePath.c_str(), ec.message()); } - boost::filesystem::remove(boost::filesystem::path(keyhashFilename), ec); + fs::remove(fs::path(keyhashFilename), ec); if (ec) { LOGE(SIM_DAEMON, "Post decryption failed: unlink %s : %s", keyhashFilename.c_str(), ec.message()); @@ -249,13 +265,13 @@ void TABinaryManager::decryptImage(StructBinaryInfo& info) { * It is very important to check for return value from this function. */ -bool TABinaryManager::unpackBinary(const string &uuid, StructBinaryInfo& info) { +bool TABinaryManager::unpackBinary(const string &uuid, const string& tapath, StructBinaryInfo& info) { TAUnpack* unpacker = TAUnpack::getInstance(); bool ret = false; - if (0 == unpacker->unpackTA(TEE_TASTORE_ROOT, uuid)) { + if (0 == unpacker->unpackTA(tapath, uuid)) { LOGD(SIM_DAEMON, "Unpacked, filling info"); // 1. Set binary info - info.path = string(TEE_TASTORE_ROOT) + uuid; + info.path = tapath + uuid; info.extractpath = string(TEE_EXTRACT_ROOT) + uuid + "-ext/"; info.imagePath = info.extractpath + uuid + ".image"; info.manifestPath = info.extractpath + uuid + ".manifest"; diff --git a/simulatordaemon/src/TABinaryManager/TABinaryManager.h b/simulatordaemon/src/TABinaryManager/TABinaryManager.h index 108fff0..b2b471d 100644 --- a/simulatordaemon/src/TABinaryManager/TABinaryManager.h +++ b/simulatordaemon/src/TABinaryManager/TABinaryManager.h @@ -64,7 +64,7 @@ private: // map < string uuid, StructBinaryInfo> map binaryMap; TABinaryManager(); - bool unpackBinary(const string &uuid, StructBinaryInfo& info); + bool unpackBinary(const string &uuid, const string& tapath, StructBinaryInfo& info); template std::string IntToHex(T i, int width = sizeof(T) * 2) { std::stringstream stream; diff --git a/simulatordaemon/src/TABinaryManager/TAUnpack.cpp b/simulatordaemon/src/TABinaryManager/TAUnpack.cpp index 28c906a..bda56dc 100644 --- a/simulatordaemon/src/TABinaryManager/TAUnpack.cpp +++ b/simulatordaemon/src/TABinaryManager/TAUnpack.cpp @@ -28,14 +28,17 @@ #include #include #include +#include #include #include #include #include #include +#include #include using namespace std; +namespace fs = boost::filesystem; /*----------------------------------------------------------------------------- * Globals @@ -66,14 +69,17 @@ int TAUnpack::unpackTA(const string& path, const string& uuid) { TAPackageHeaderV2 packageHeader; memset(&packageHeader, 0, sizeof(TAPackageHeaderV2)); - // Create directory for TA extracted files + // Re-create directory for TA extracted files string extract_dir_path = TEE_EXTRACT_ROOT + uuid + "-ext/"; - struct stat info; - if (stat(extract_dir_path.c_str(), &info) != 0) { - if (0 != mkdir(extract_dir_path.c_str(), 0777)) { - LOGE(SIM_DAEMON, "mkdir failed %s %s", extract_dir_path.c_str(), strerror(errno)); - return -1; - } + boost::system::error_code ec; + fs::remove_all(extract_dir_path, ec); + if (ec != 0){ + LOGE(SIM_DAEMON, "remove_all failed %s %s (trying to continue)", extract_dir_path.c_str(), ec.message().c_str()); + } + fs::create_directory(extract_dir_path, ec); + if (ec != 0){ + LOGE(SIM_DAEMON, "create_directory failed %s %s", extract_dir_path.c_str(), ec.message().c_str()); + return -1; } // Open TA package file