/**
- * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
-#if !defined(_TAFACTORY_H)
+#ifndef _TAFACTORY_H
#define _TAFACTORY_H
/*-----------------------------------------------------------------------------
#include "ISession.h"
#include "TAInstance.h"
#include "TABinaryManager.h"
+#include "UUIDUtils.h"
/*-----------------------------------------------------------------------------
* Class definitions
class TAFactory {
public:
pthread_rwlock_t mTAInstanceMapLock;
- multimap<string, TAInstancePtr> mTAInstanceMap;
+ multimap<TEEC_UUID, TAInstancePtr, UUIDComparator> mTAInstanceMap;
static TAFactory* getInstance();
TAInstancePtr getTAInstance(TEEC_UUID TAUUID, ISession* session);
private:
pthread_rwlock_t instIDLock;
uint32_t InstID;
TAFactory();
- bool checkIfTARunning(string TAUUID);
- TAInstancePtr createUninitalizedTAInstance(string TAUUID, ISession* session);
- bool launchTA(string TAUUID, std::stringstream& str, bool debug, pid_t& pid);
+ bool checkIfTARunning(TEEC_UUID TAUUID);
+ TAInstancePtr createUninitalizedTAInstance(TEEC_UUID TAUUID, ISession* session);
+ bool launchTA(TEEC_UUID TAUUID, std::stringstream& str, bool debug, pid_t& pid);
static void* waitForChild(void *pid);
void cleanupTAInstance(pid_t PID);
~TAFactory();
std::memcpy(&cmd, data.data(), sizeof(cmd));
std::string portString = std::to_string(cmd.port);
std::string uuidString = UUIDToString(cmd.uuid);
- ret = binaryManager->setPort(uuidString, portString);
+ ret = binaryManager->setPort(cmd.uuid, portString);
if (!ret) {
reply.status = CTL_REPLY_SUCCESS;
LOGI(SIM_DAEMON, "Set debug port of UUID %s to %s",
/**
- * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
ISession* session = it->second;
(session->getTAInstance())->takeSessionMapLock();
pSessionMap->erase(it);
- multimap<string, TAInstancePtr>::iterator itTAMap;
pthread_rwlock_wrlock(&TAFact->mTAInstanceMapLock);
- for (itTAMap = TAFact->mTAInstanceMap.begin();
+ for (auto itTAMap = TAFact->mTAInstanceMap.begin();
itTAMap != TAFact->mTAInstanceMap.end(); ++itTAMap) {
if (itTAMap->second == session->getTAInstance()) {
if (pSessionMap->size() == 0) {
LOGE(SIM_DAEMON, "Creating TABinaryManager Instance FAILED - ");
return TEEC_ERROR_GENERIC;
}
- string TAUUID = UUIDToString(data.uuid);
- string argvPort = TABin->getPort(TAUUID);
-
- string TAName(TAUUID);
- std::transform(TAName.begin(), TAName.end(), TAName.begin(), ::toupper);
+ string argvPort = TABin->getPort(data.uuid);
if (argvPort != "") {
pthread_rwlock_wrlock(&TAFact->mTAInstanceMapLock);
- multimap<string, TAInstancePtr>::iterator itr;
- itr = TAFact->mTAInstanceMap.find(TAUUID);
+ auto itr = TAFact->mTAInstanceMap.find(data.uuid);
if (itr != TAFact->mTAInstanceMap.end()) {
mTAInstance = itr->second;
LOGD(SIM_DAEMON, "KILL pid = %d", mTAInstance->getPID());
if (TEEC_SUCCESS != result) { // failure
LOGE(SIM_DAEMON, "Create TA entry point FAILED");
pthread_rwlock_wrlock(&TAFact->mTAInstanceMapLock);
- multimap<string, TAInstancePtr>::iterator it;
- for (it = TAFact->mTAInstanceMap.begin();
+ for (auto it = TAFact->mTAInstanceMap.begin();
it != TAFact->mTAInstanceMap.end(); ++it) {
if (it->second == mTAInstance) {
// Kill the TA process
mTAInstance->takeSessionMapLock();
mTAInstance->eraseSessionMap(mSessionID);
- multimap<string, TAInstancePtr>::iterator itTAMap;
-
/* Check if this is the last session associated with the TA Instance.
* If it is the last instance then check for KeepAlive flag.
*/
pthread_rwlock_wrlock(&TAFact->mTAInstanceMapLock);
- for (itTAMap = TAFact->mTAInstanceMap.begin();
+ for (auto itTAMap = TAFact->mTAInstanceMap.begin();
itTAMap != TAFact->mTAInstanceMap.end(); ++itTAMap) {
if (itTAMap->second == mTAInstance) {
/* If this is the last session associated with TA then check for
*-----------------------------------------------------------------------------*/
TABinaryManager *TABinaryManager::instance = NULL;
pthread_rwlock_t binaryMapLock;
-map<string, StructBinaryInfo> binaryMap;
+map<TEEC_UUID, StructBinaryInfo, UUIDComparator> binaryMap;
/*-----------------------------------------------------------------------------
* Member functions
* This function add TA to BinaryManager if it exists.
* @return On successful completion of above operations returns true else false.
*/
-bool TABinaryManager::initTA(const string &uuid) {
+bool TABinaryManager::initTA(const TEEC_UUID &uuid) {
LOGD(SIM_DAEMON, "Entry");
std::vector<string> paths;
paths.push_back(TEE_EMBEDDED_TASTORE_ROOT);
paths.push_back(TEE_TASTORE_ROOT TEE_DOWNLOADED_TA_SUFFIX);
+ string uuidStr = UUIDToString(uuid);
string tapath;
for(auto const& p: paths) {
if (p.empty()) continue; // ignore empty paths
- string path_to_file = p + uuid;
+ string path_to_file = p + uuidStr;
boost::system::error_code ec;
if (fs::exists(path_to_file, ec)) {
tapath = p;
}
if (tapath.empty()) {
- LOGE(SIM_DAEMON, "Cannot find TA: %s", uuid.c_str());
+ LOGE(SIM_DAEMON, "Cannot find TA: %s", uuidStr.c_str());
return false;
}
* It is very important to check for return value from this function.
*/
-bool TABinaryManager::unpackBinary(const string &uuid, const string& tapath, StructBinaryInfo& info) {
+bool TABinaryManager::unpackBinary(const TEEC_UUID &uuid, const string& tapath, StructBinaryInfo& info) {
TAUnpack* unpacker = TAUnpack::getInstance();
+ string uuidStr = UUIDToString(uuid);
bool ret = false;
- if (0 == unpacker->unpackTA(tapath, uuid)) {
- LOGD(SIM_DAEMON, "Unpacked TA %s from %s", uuid.c_str(), tapath.c_str());
+ if (0 == unpacker->unpackTA(tapath, uuidStr)) {
+ LOGD(SIM_DAEMON, "Unpacked TA %s from %s", uuidStr.c_str(), tapath.c_str());
// 1. Set binary info
- info.path = tapath + uuid;
- info.extractpath = string(TEE_EXTRACT_ROOT) + uuid + "-ext/";
- info.imagePath = info.extractpath + uuid + ".image";
- info.manifestPath = info.extractpath + uuid + ".manifest";
+ info.path = tapath + uuidStr;
+ info.extractpath = string(TEE_EXTRACT_ROOT) + uuidStr + "-ext/";
+ info.imagePath = info.extractpath + uuidStr + ".image";
+ info.manifestPath = info.extractpath + uuidStr + ".manifest";
// 2. Parse manifest and store results
info.manifest.processXML(info.manifestPath);
* @return -1 if uuid is not found else on success 0
*/
-int TABinaryManager::isSingleInstance(string uuid, bool &SingleInstance) {
+int TABinaryManager::isSingleInstance(TEEC_UUID uuid, bool &SingleInstance) {
pthread_rwlock_wrlock(&binaryMapLock);
- map<string, StructBinaryInfo>::iterator it = binaryMap.find(uuid);
+ auto it = binaryMap.find(uuid);
StructBinaryInfo value;
int ret = -1;
if (it != binaryMap.end()) {
* @return -1 if uuid is not found else on success 0
*/
-int TABinaryManager::isKeepAlive(string uuid, bool &KeepAlive) {
+int TABinaryManager::isKeepAlive(TEEC_UUID uuid, bool &KeepAlive) {
pthread_rwlock_wrlock(&binaryMapLock);
- map<string, StructBinaryInfo>::iterator it = binaryMap.find(uuid);
+ auto it = binaryMap.find(uuid);
StructBinaryInfo value;
int ret = -1;
if (it != binaryMap.end()) {
* @param[out] isMultipleSession returns value from this parameter.
* @return -1 if uuid is not found else on success 0
*/
-int TABinaryManager::isMultipleSession(string uuid, bool &MultipleSession) {
+int TABinaryManager::isMultipleSession(TEEC_UUID uuid, bool &MultipleSession) {
pthread_rwlock_wrlock(&binaryMapLock);
- map<string, StructBinaryInfo>::iterator it = binaryMap.find(uuid);
+ auto it = binaryMap.find(uuid);
StructBinaryInfo value;
int ret = -1;
if (it != binaryMap.end()) {
* @param uuid UUID of TA
* @return Empty string if UUID doesn't exist, else path to TA
*/
-string TABinaryManager::getImagePath(string uuid) {
+string TABinaryManager::getImagePath(TEEC_UUID uuid) {
pthread_rwlock_wrlock(&binaryMapLock);
- map<string, StructBinaryInfo>::iterator it = binaryMap.find(uuid);
+ auto it = binaryMap.find(uuid);
StructBinaryInfo value;
string ret = "";
if (it != binaryMap.end()) {
* @param uuid UUID of TA
* @return NULL pointer if
*/
-const TAManifest* TABinaryManager::getManifest(string uuid) {
+const TAManifest* TABinaryManager::getManifest(TEEC_UUID uuid) {
pthread_rwlock_wrlock(&binaryMapLock);
- map<string, StructBinaryInfo>::iterator it = binaryMap.find(uuid);
+ auto it = binaryMap.find(uuid);
TAManifest *returnValue = NULL;
if (it != binaryMap.end()) {
//element found;
* @param uuid UUID of TA
* @return NULL pointer if
*/
-string TABinaryManager::getPort(string uuid) {
+string TABinaryManager::getPort(TEEC_UUID uuid) {
pthread_rwlock_wrlock(&binaryMapLock);
- map<string, StructBinaryInfo>::iterator it = binaryMap.find(uuid);
+ auto it = binaryMap.find(uuid);
string returnValue = "";
if (it != binaryMap.end()) {
returnValue = it->second.port;
return returnValue;
}
-int TABinaryManager::setPort(string uuid, string port) {
+int TABinaryManager::setPort(TEEC_UUID uuid, string port) {
int ret;
pthread_rwlock_wrlock(&binaryMapLock);
- map<string, StructBinaryInfo>::iterator it = binaryMap.find(uuid);
+ auto it = binaryMap.find(uuid);
if (it != binaryMap.end()) {
it->second.port = port;
ret = 0;
#include "TAManifest.h"
#include "TAUnpack.h"
#include "tee_internal_api.h"
+#include "UUIDUtils.h"
using namespace std;
private:
static TABinaryManager *instance;
std::string base64_chars;
- // map < string uuid, StructBinaryInfo>
- map<string, StructBinaryInfo> binaryMap;
+ map<TEEC_UUID, StructBinaryInfo, UUIDComparator> binaryMap;
TABinaryManager();
- bool unpackBinary(const string &uuid, const string& tapath, StructBinaryInfo& info);
+ bool unpackBinary(const TEEC_UUID &uuid, const string& tapath, StructBinaryInfo& info);
void decryptImage(StructBinaryInfo& info);
string base64_decode(std::string const& encoded_string);
bool is_base64(unsigned char c);
*/
pthread_mutex_t taLock;
static TABinaryManager* getInstance();
- bool initTA (const string &uuid);
+ bool initTA (const TEEC_UUID &uuid);
/*
* Query functions on Binary Manager
*/
- int isSingleInstance(string uuid, bool &SingleInstance);
- int isMultipleSession(string uuid, bool &MultipleSession);
- string getImagePath(string uuid);
- const TAManifest* getManifest(string uuid);
- string getPort(string uuid);
- int isKeepAlive(string uuid, bool &KeepAlive);
+ int isSingleInstance(TEEC_UUID uuid, bool &SingleInstance);
+ int isMultipleSession(TEEC_UUID uuid, bool &MultipleSession);
+ string getImagePath(TEEC_UUID uuid);
+ const TAManifest* getManifest(TEEC_UUID uuid);
+ string getPort(TEEC_UUID uuid);
+ int isKeepAlive(TEEC_UUID uuid, bool &KeepAlive);
/*
* TA property setters
*/
- int setPort(string uuid, string port);
+ int setPort(TEEC_UUID uuid, string port);
virtual ~TABinaryManager();
};
/**
- * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "TAManifest.h"
#include "TAUnpack.h"
#include <config.h>
+#include "tee_client_api.h"
using namespace std;
cout
<< "4. TA Binary Manager (Read UUID List, unpack all of the binaries, keep meta data ready)"
<< std::endl;
-
+ TEEC_UUID[3] testUUIDs = {
+ {
+ .timeLow = 0,
+ .timeMid = 0,
+ .timeHiAndVersion = 0,
+ .clockSeqAndNode = {0, 0, 0, 0, 0, 0, 0, 0xc7},
+ },
+ {
+ .timeLow = 0,
+ .timeMid = 0,
+ .timeHiAndVersion = 0,
+ .clockSeqAndNode = {0, 0, 0, 0, 0, 0x12, 0x34, 0xd5},
+ },
+ {
+ .timeLow = 0,
+ .timeMid = 0,
+ .timeHiAndVersion = 0,
+ .clockSeqAndNode = {0, 0, 0, 0, 0, 0x45, 0x67, 0xc8},
+ },
+ }
try {
while (inp != 0) {
+
std::cout << "[SIM_DAEMON] Enter Choice: " << std::endl;
scanf("%d", &inp);
std::cout << "[SIM_DAEMON] Binary Manager successfully initialized"
<< std::endl;
std::cout
- << "[SIM_DAEMON] Image Path of 0000000000000000000000c7: "
- << bm->getImagePath("0000000000000000000000c7") << std::endl;
+ << "[SIM_DAEMON] Image Path of 0000-0000-0000-0000000000c7: "
+ << bm->getImagePath(testUUIDs[0]) << std::endl;
std::cout
- << "[SIM_DAEMON] Image Path of 0000000000000000001234d5: "
- << bm->getImagePath("0000000000000000001234d5") << std::endl;
+ << "[SIM_DAEMON] Image Path of 0000-0000-0000-0000001234d5: "
+ << bm->getImagePath(testUUIDs[1]) << std::endl;
std::cout
- << "[SIM_DAEMON] Image Path of 0000000000000000004567c8: "
- << bm->getImagePath("0000000000000000004567c8") << std::endl;
- bm->getManifest("0000000000000000004567c8")->printProcessedData();
+ << "[SIM_DAEMON] Image Path of 0000-0000-0000-0000004567c8: "
+ << bm->getImagePath(testUUIDs[2]) << std::endl;
+ bm->getManifest(testUUIDs[2])->printProcessedData();
break;
}
#include "SecurityContext.h"
#include "UUIDUtils.h"
#include "ResponseCommands/ResMakeCommand.h"
+#include "tee_client_api.h"
/*-----------------------------------------------------------------------------
// Get TA Binary Manager instance
TABinaryManager *TABin = TABinaryManager::getInstance();
- // Get TEEC_UUID format uuid converted to string form
- string TAUUID = UUIDToString(uuid);
- // Change to upper char. TA list has upper char.
- locale loc;
- for (size_t i = 0; i < TAUUID.length(); ++i)
- TAUUID[i] = toupper(TAUUID[i], loc);
-
-
- if ((!checkIfTARunning(TAUUID))
- || ((TABin->isSingleInstance(TAUUID, result) == 0) && (result == false))) {
+ if ((!checkIfTARunning(uuid))
+ || ((TABin->isSingleInstance(uuid, result) == 0) && (result == false))) {
/* TA instance is not already alive or is Multi Instance, Create a new TA
* Instance
*/
- TAInst = createUninitalizedTAInstance(TAUUID, session);
+ TAInst = createUninitalizedTAInstance(uuid, session);
if (!TAInst == true) {
LOGE(SIM_DAEMON, "Creating Trusted Application Instance FAILED");
return TAInstancePtr();
}
- } else if ((TABin->isSingleInstance(TAUUID, result) == 0)
+ } else if ((TABin->isSingleInstance(uuid, result) == 0)
&& (result == true)) {
// TA is Single Instance and alive
- if (TABin->isMultipleSession(TAUUID, result) < 0) {
+ if (TABin->isMultipleSession(uuid, result) < 0) {
LOGE(SIM_DAEMON, "TA not in list");
return TAInstancePtr();
} else {
- multimap<string, TAInstancePtr>::iterator it;
// Find alive TA Instance in TA Factory's Instance Map
- it = mTAInstanceMap.find(TAUUID);
+ auto it = mTAInstanceMap.find(uuid);
if (it != mTAInstanceMap.end()) {
TAInst = it->second;
TAInst->takeSessionMapLock();
/**
* Check if TA already alive. Return true if it is present in the TA Instance
* Map else return false
- * @param TAUUID TA UUID in string form
+ * @param TAUUID TA UUID
*/
-bool TAFactory::checkIfTARunning(string TAUUID) {
+bool TAFactory::checkIfTARunning(TEEC_UUID TAUUID) {
LOGD(SIM_DAEMON, "Entry");
- bool result = false;
-
- // Find TA UUID in the TA Instance Map
- multimap<string, TAInstancePtr>::iterator itr;
- itr = mTAInstanceMap.find(TAUUID);
- if (itr != mTAInstanceMap.end()) result = true;
- return result;
+ auto itr = mTAInstanceMap.find(TAUUID);
+ return itr != mTAInstanceMap.end();
}
/**
* Create a new TA Instance by launching the TA specified by TA UUID
- * @param TAUUID TA UUID in string form
+ * @param TAUUID TA UUID
* @param session Session which requested the TA Instance to be associated
* with it
*/
-TAInstancePtr TAFactory::createUninitalizedTAInstance(string TAUUID,
+TAInstancePtr TAFactory::createUninitalizedTAInstance(TEEC_UUID TAUUID,
ISession* session) {
LOGD(SIM_DAEMON, "Entry");
TEEC_Result result = TEEC_ERROR_COMMUNICATION;
TABinaryManager *TABin = TABinaryManager::getInstance();
+
+ /* FIXME - determine if the socket cares about case */
+ string uuidStr = UUIDToString(TAUUID);
+ std::transform(uuidStr.begin(), uuidStr.end(), uuidStr.begin(), ::toupper);
+
pthread_rwlock_wrlock(&instIDLock);
/* Generate the endpoint name using UUID and Instance ID to be sent to
* TEEStub for socket connection
*/
std::stringstream str;
- str << TAUUID << "-";
+ str << uuidStr << "-";
str << InstID;
if (launchTA(TAUUID, str, debug, pid)) {
pthread_rwlock_unlock(&instIDLock);
// Add the TAInstance in the TA Factory's TA Instance Map
- mTAInstanceMap.insert(pair<string, TAInstancePtr>(TAUUID, TAInst));
+ mTAInstanceMap.insert(pair<TEEC_UUID, TAInstancePtr>(TAUUID, TAInst));
return TAInst;
}
pthread_rwlock_unlock(&instIDLock);
TAInstancePtr Inst;
// Find the TA instance associated with the argument PID
- multimap<string, TAInstancePtr>::iterator itInstanceMap;
- for (itInstanceMap = mTAInstanceMap.begin();
+ for (auto itInstanceMap = mTAInstanceMap.begin();
itInstanceMap != mTAInstanceMap.end(); itInstanceMap++) {
if (itInstanceMap->second->getPID() == PID) {
Inst = itInstanceMap->second;
Inst->cleanup();
// Remove the TA Instance from the TA Instance Map maintained in TA Factory
pthread_rwlock_wrlock(&mTAInstanceMapLock);
- multimap<string, TAInstancePtr>::iterator itMap;
- for (itMap = mTAInstanceMap.begin(); itMap != mTAInstanceMap.end();
+ for (auto itMap = mTAInstanceMap.begin(); itMap != mTAInstanceMap.end();
++itMap) {
if (itMap->second == Inst) {
mTAInstanceMap.erase(itMap);
/**
* Launch the TA instance
- * @param TAUUID TA UUID is string format
+ * @param TAUUID TA UUID
* @param str string containing the endpoint name to be passed as an argument
* to TA while launching
* @param debug debug flag
* @param pid PID to be update for launched TA
*/
-bool TAFactory::launchTA(string TAUUID, std::stringstream& str, bool debug,
+bool TAFactory::launchTA(TEEC_UUID TAUUID, std::stringstream& str, bool debug,
pid_t& pid) {
int32_t result = -1;
// Get TABinaryManager instance
TABinaryManager *TABin = TABinaryManager::getInstance();
+ string uuidStr = UUIDToString(TAUUID);
// Get TA Image path for launching
string argvPath = "";
if (TABin->initTA(TAUUID)) {
}
ret = posix_spawn_file_actions_addopen(&child_fd_actions, 1,
- (TEE_TALOG_ROOT + TAUUID + ".log").c_str(),
+ (TEE_TALOG_ROOT + uuidStr + ".log").c_str(),
O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0644);
if (ret != 0) {
LOGE(SIM_DAEMON, "posix_spawn_file_actions_addopen failed");
const TAManifest* srcTAManifest;
const TAManifest* dstTAManifest;
- string source_uuid, dest_uuid;
string srcCreateDomain;
string dstAllowedDomain;
unsigned int dstAllowedDomainCount;
memcpy(&src, &data.source, sizeof(TEEC_UUID));
memcpy(&dst, &data.destination, sizeof(TEEC_UUID));
- source_uuid = UUIDToString(src);
- dest_uuid = UUIDToString(dst);
+ std::string srcStr = UUIDToString(src);
+ std::string dstStr = UUIDToString(dst);
- std::transform(source_uuid.begin(), source_uuid.end(), source_uuid.begin(), ::toupper);
- std::transform(dest_uuid.begin(), dest_uuid.end(), dest_uuid.begin(), ::toupper);
-
- dstTAManifest = TABin->getManifest(dest_uuid);
- srcTAManifest = TABin->getManifest(source_uuid);
+ dstTAManifest = TABin->getManifest(dst);
+ srcTAManifest = TABin->getManifest(src);
if(srcTAManifest == NULL || dstTAManifest == NULL) {
- LOGE(SIM_DAEMON, "Can`t find TA Manifest - source_uuid(%s), destination_uuid(%s)", source_uuid.c_str(), dest_uuid.c_str());
+ LOGE(SIM_DAEMON, "Can`t find TA Manifest - source_uuid(%s), destination_uuid(%s)", srcStr.c_str(), dstStr.c_str());
return TEEC_ERROR_ACCESS_DENIED;
}