Embedded path configuration 31/170231/3
authorKrzysztof Dynowski <k.dynowski@samsung.com>
Wed, 14 Feb 2018 13:07:58 +0000 (14:07 +0100)
committerKrzysztof Dynowski <k.dynowski@samsung.com>
Wed, 21 Feb 2018 15:55:44 +0000 (16:55 +0100)
Change-Id: I7ae530892a878a5009cab75e0610056c42f4ce59

include/include/config.h
simulatordaemon/src/TABinaryManager/TABinaryManager.cpp
simulatordaemon/src/TABinaryManager/TABinaryManager.h
simulatordaemon/src/TABinaryManager/TAUnpack.cpp

index 97abb4b..e10eb5c 100644 (file)
@@ -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
index c609831..1a37cfb 100644 (file)
@@ -36,6 +36,8 @@
 #include <algorithm>
 #include <boost/filesystem.hpp>
 #include <config.h>
+
+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";
index 108fff0..b2b471d 100644 (file)
@@ -64,7 +64,7 @@ private:
        // map < string uuid, StructBinaryInfo>
        map<string, StructBinaryInfo> binaryMap;
        TABinaryManager();
-       bool unpackBinary(const string &uuid, StructBinaryInfo& info);
+       bool unpackBinary(const string &uuid, const string& tapath, StructBinaryInfo& info);
        template<typename T>
        std::string IntToHex(T i, int width = sizeof(T) * 2) {
                std::stringstream stream;
index 28c906a..bda56dc 100644 (file)
 #include <iostream>
 #include <fstream>
 #include <memory>
+#include <boost/filesystem/operations.hpp>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <config.h>
 #include <log.h>
 
 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