#include <string.h>
#include <errno.h>
#include <cstdio>
+#include <sstream>
+#include<iostream>
#include <memory>
#include <dpl/scoped_fclose.h>
#include <dpl/wrt-dao-ro/global_config.h>
#include <dpl/string.h>
+#include <FBaseByteBuffer.h>
+#include <security/FSecCrypto_TrustZoneService.h>
#include <widget_install/job_widget_install.h>
#include <widget_install/widget_install_context.h>
#include <widget_install/widget_install_errors.h>
using namespace WrtDB;
-using namespace WRTEncryptor;
+
+extern "C"
+{
+ void InitWebAppInfo(const char* appId, const char* rootPath);
+}
namespace {
-const std::size_t ENCRYPTION_CHUNK_MAX_SIZE = 1008; // bytes
+const std::size_t ENCRYPTION_CHUNK_MAX_SIZE = 4096; // bytes
+const std::size_t ENCRYPTION_DEC_CHUNK_SIZE = 4; // bytes
+static bool initWebApp = false;
std::set<std::string>& getSupportedForEncryption()
{
if (NULL == result)
{
- ThrowMsg(Jobs::WidgetInstall::Exceptions::InternalError,
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::EncryptionFailed,
"Could not open file " << path);
}
{
if (EINTR != error)
{
- ThrowMsg(Jobs::WidgetInstall::Exceptions::InternalError,
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::ErrorExternalInstallingFailure,
"Error while reading data" <<
" [" << DPL::GetErrnoString(error) << "]");
}
if ((bytesWritten != bytesToWrite) && (EINTR != errno))
{
int error = errno;
- ThrowMsg(Jobs::WidgetInstall::Exceptions::InternalError,
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::EncryptionFailed,
"Error while writing data" <<
" [" << DPL::GetErrnoString(error) << "]");
}
} while ((bytesWritten != bytesToWrite) && (EINTR == errno));
}
+
+/*
+ * get encrypted string from trustzone
+*/
+Tizen::Base::ByteBuffer* EncryptChunkByTrustZone(
+ Tizen::Base::ByteBuffer* appInfo,
+ const unsigned char *plainBuffer,
+ int pBufSize)
+{
+ using namespace Tizen::Base;
+ if(!initWebApp)
+ {
+ char* pAppId = null;
+ pAppId = (char*)calloc(appInfo->GetRemaining()+1, 1);
+ memcpy(pAppId, appInfo->GetPointer(), appInfo->GetRemaining());
+ InitWebAppInfo(pAppId, "");
+ free (pAppId);
+ initWebApp = true;
+ }
+
+ Tizen::Security::Crypto::_TrustZoneService* pInstance;
+ pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance();
+
+ ByteBuffer pBuf;
+ pBuf.Construct(pBufSize);
+ const byte *pByte = reinterpret_cast<const byte*>(plainBuffer);
+ if (pBuf.SetArray(pByte, 0, pBufSize) != E_SUCCESS) {
+ LogDebug("Couldnot set pBuf");
+ return NULL;
+ }
+ pBuf.Flip();
+
+ return pInstance->_TrustZoneService::EncryptN(*appInfo, pBuf);
+}
+
+Tizen::Base::ByteBuffer* TEST_dec(
+ Tizen::Base::ByteBuffer* appInfo,
+ const unsigned char *plainBuffer,
+ int pBufSize)
+{
+ using namespace Tizen::Base;
+
+ Tizen::Security::Crypto::_TrustZoneService* pInstance;
+ pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance();
+
+ ByteBuffer pBuf;
+ pBuf.Construct(pBufSize);
+ const byte *pByte = reinterpret_cast<const byte*>(plainBuffer);
+ if (pBuf.SetArray(pByte, 0, pBufSize) != E_SUCCESS) {
+ LogDebug("Couldnot set pBuf");
+ return NULL;
+ }
+ pBuf.Flip();
+
+ return pInstance->_TrustZoneService::DecryptN(*appInfo, pBuf);
+}
}
namespace Jobs {
namespace WidgetInstall {
TaskEncryptResource::TaskEncryptResource(InstallerContext& context) :
DPL::TaskDecl<TaskEncryptResource>(this),
- m_context(context),
- m_resEnc(NULL)
+ m_context(context)
{
AddStep(&TaskEncryptResource::StepEncryptResource);
}
void TaskEncryptResource::StepEncryptResource()
{
LogDebug("Step Encrypt resource");
- m_resEnc = new ResourceEncryptor;
EncryptDirectory(m_context.locations->getTemporaryRootDir());
m_context.job->UpdateProgress(
int error = errno;
LogWarning(__PRETTY_FUNCTION__ << ": fts_open failed with error: "
<< strerror(error));
- ThrowMsg(Exceptions::InternalError, "Error reading directory: "
+ ThrowMsg(Exceptions::EncryptionFailed, "Error reading directory: "
<< path);
}
<< ftsent->fts_path
<< " with error: "
<< strerror(ftsent->fts_errno));
- ThrowMsg(Exceptions::InternalError, "Error reading file");
+ ThrowMsg(Exceptions::EncryptionFailed, "Error reading file");
break;
}
}
void TaskEncryptResource::EncryptFile(const std::string &fileName)
{
- try
- {
- LogDebug("Encrypt file: " << fileName);
- std::string encFile = fileName + ".enc";
+ LogDebug("Encrypt file: " << fileName);
+ std::string encFile = fileName + ".enc";
- struct stat info;
- memset(&info, 0, sizeof(info));
- if (stat(fileName.c_str(), &info) != 0)
- {
- int error = errno;
- ThrowMsg(Exceptions::InternalError,
- "Could not access file " << fileName <<
- "[" << DPL::GetErrnoString(error) << "]");
- }
- const std::size_t fileSize = info.st_size;
-
- DPL::ScopedFClose inFile(openFile(fileName, "r"));
- DPL::ScopedFClose outFile(openFile(encFile, "w"));
+ struct stat info;
+ memset(&info, 0, sizeof(info));
+ if (stat(fileName.c_str(), &info) != 0)
+ {
+ int error = errno;
+ ThrowMsg(Exceptions::EncryptionFailed,
+ "Could not access file " << fileName <<
+ "[" << DPL::GetErrnoString(error) << "]");
+ }
+ const std::size_t fileSize = info.st_size;
- const std::size_t chunkSize = (fileSize > ENCRYPTION_CHUNK_MAX_SIZE
- ? ENCRYPTION_CHUNK_MAX_SIZE : fileSize);
- const int maxBlockSize = m_resEnc->GetBlockSize(chunkSize);
+ DPL::ScopedFClose inFile(openFile(fileName, "r"));
+ DPL::ScopedFClose outFile(openFile(encFile, "w"));
- std::unique_ptr<unsigned char[]> inChunk(new unsigned char[chunkSize]);
- std::unique_ptr<unsigned char[]> outChunk;
+ const std::size_t chunkSize = (fileSize > ENCRYPTION_CHUNK_MAX_SIZE
+ ? ENCRYPTION_CHUNK_MAX_SIZE : fileSize);
- std::size_t bytesRead = 0;
- int curBlockSize = 0;
- do
- {
- bytesRead = readBytes(inChunk.get(), chunkSize, inFile.Get());
- if (0 != bytesRead) {
- int decBufSize = m_resEnc->EncryptChunkByTrustZone(DPL::ToUTF8String(
- m_context.widgetConfig.tzAppid),
- inChunk.get(), bytesRead);
- outChunk.reset(new unsigned char[decBufSize]);
- m_resEnc->getEncStringByTrustZone(outChunk.get());
-
- writeBytes(outChunk.get(), decBufSize, outFile.Get());
- }
+ std::unique_ptr<unsigned char[]> inChunk(new unsigned char[chunkSize]);
+ std::unique_ptr<unsigned char[]> outChunk;
- } while (0 == std::feof(inFile.Get()));
+ std::size_t bytesRead = 0;
+ using namespace Tizen::Base;
- LogDebug("File encrypted successfully");
+ std::string pkgid =
+ DPL::ToUTF8String(m_context.widgetConfig.tzAppid).c_str();
+ const byte *b_pkgid = reinterpret_cast<const byte*>(
+ pkgid.c_str());
+ ByteBuffer appInfo;
+ appInfo.Construct(pkgid.length());
- outFile.Reset();
- inFile.Reset();
+ if (appInfo.SetArray(b_pkgid, 0, pkgid.length()) != E_SUCCESS) {
+ LogDebug("Couldnot set appInfo");
+ return;
+ }
- LogDebug("Remove plain-text file: " << fileName);
- if (0 != unlink(fileName.c_str()))
- {
- Throw(Exceptions::InternalError);
- }
+ appInfo.Flip();
- LogDebug("Rename encrypted file");
- if (0 != std::rename(encFile.c_str(), fileName.c_str()))
- {
- Throw(Exceptions::InternalError);
+ do
+ {
+ bytesRead = readBytes(inChunk.get(), chunkSize, inFile.Get());
+ if (0 != bytesRead) {
+ ByteBuffer *getBuffer = EncryptChunkByTrustZone(
+ &appInfo,
+ inChunk.get(), bytesRead);
+ int decBufSize = getBuffer->GetRemaining();
+
+ outChunk.reset(new unsigned char[decBufSize]);
+ memcpy(outChunk.get(), getBuffer->GetPointer(), getBuffer->GetRemaining());
+ getBuffer->Reset();
+
+ char writeSize[ENCRYPTION_DEC_CHUNK_SIZE];
+ memset(writeSize, 0x00, ENCRYPTION_DEC_CHUNK_SIZE);
+ std::stringstream toString;
+ toString << decBufSize;
+ strncpy(writeSize, toString.str().c_str(), toString.str().length());
+
+ writeBytes((unsigned char*)writeSize,
+ ENCRYPTION_DEC_CHUNK_SIZE, outFile.Get());
+ writeBytes(outChunk.get(), decBufSize, outFile.Get());
}
- std::string realPath = fileName;
- realPath.replace(0,
- m_context.locations->getTemporaryRootDir().length(),
- m_context.locations->getSourceDir());
+ } while (0 == std::feof(inFile.Get()));
- WrtDB::EncryptedFileInfo fileInfo;
- fileInfo.fileName = DPL::FromUTF8String(realPath);
- fileInfo.fileSize = fileSize;
+ outFile.Reset();
+ inFile.Reset();
- m_context.widgetConfig.encryptedFiles.insert(fileInfo);
- }
- Catch (Exceptions::InternalError)
+ LogDebug("File encrypted successfully");
+ LogDebug("Remove plain-text file: " << fileName);
+ if (0 != unlink(fileName.c_str()))
{
- ReThrowMsg(Exceptions::ExtractFileFailed, fileName);
+ Throw(Exceptions::EncryptionFailed);
}
- Catch (ResourceEncryptor::Exception::Base)
+
+ LogDebug("Rename encrypted file");
+ if (0 != std::rename(encFile.c_str(), fileName.c_str()))
{
- ReThrowMsg(Exceptions::ExtractFileFailed, fileName);
+ Throw(Exceptions::EncryptionFailed);
}
+
+ std::string realPath = fileName;
+ realPath.replace(0,
+ m_context.locations->getTemporaryRootDir().length(),
+ m_context.locations->getSourceDir());
+
+ WrtDB::EncryptedFileInfo fileInfo;
+ fileInfo.fileName = DPL::FromUTF8String(realPath);
+ fileInfo.fileSize = fileSize;
+
+ m_context.widgetConfig.encryptedFiles.insert(fileInfo);
}
} //namespace WidgetInstall