From 739f146ccf5ac98f46c1dee212f0c61beecdbd3a Mon Sep 17 00:00:00 2001 From: Soyoung Kim Date: Fri, 15 Mar 2013 17:27:43 +0900 Subject: [PATCH] Fixed decryption logic [Issue#] N/A [Problem] occur crash during decryption. [Cause] should initialize app framework of osp during decryption. [Solution] call InitWebAppInfo() api before decryption. [SCMRequest] N/A --- src/view/webkit/bundles/wrt-wk2-bundle.cpp | 105 +++++++++++++++++------------ src/view/webkit/bundles/wrt-wk2-bundle.h | 2 - 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/view/webkit/bundles/wrt-wk2-bundle.cpp b/src/view/webkit/bundles/wrt-wk2-bundle.cpp index c374f9e..de060c9 100644 --- a/src/view/webkit/bundles/wrt-wk2-bundle.cpp +++ b/src/view/webkit/bundles/wrt-wk2-bundle.cpp @@ -72,6 +72,9 @@ // URI localization on WebProcess side #include "bundle_uri_handling.h" +extern "C" { +void InitWebAppInfo(const char* appId, const char* rootPath); +} namespace { const char * const uriChangedMessageName = "uri_changed_msg"; @@ -91,7 +94,37 @@ const char * const PHP_MIME = "application/x-php"; const char * const VIEWMODE_TYPE_FULLSCREEN = "fullscreen"; const char * const VIEWMODE_TYPE_MAXIMIZED = "maximized"; const std::size_t FILE_BUF_MAX_SIZE = 1024; // bytes -const std::size_t PLAIN_CHUNK_SIZE = 1008; // bytes +static bool m_initWebApp = false; + +Tizen::Base::ByteBuffer *DecryptChunkByTrustZone( + Tizen::Base::ByteBuffer *appInfo, + const unsigned char *inBuffer, + int inBufSize) +{ + using namespace Tizen::Base; + + if (!m_initWebApp) { + char* pAppId = null; + pAppId = (char*)calloc(appInfo->GetRemaining()+1, 1); + memcpy(pAppId, appInfo->GetPointer(), appInfo->GetRemaining()); + + InitWebAppInfo(pAppId, ""); + free (pAppId); + m_initWebApp = true; + } + + Tizen::Security::Crypto::_TrustZoneService* pInstance; + pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance(); + + ByteBuffer pBuf; + pBuf.Construct(inBufSize); + const byte *pByte = reinterpret_cast(inBuffer); + pBuf.SetArray(pByte, 0, inBufSize); + pBuf.Flip(); + + ByteBuffer *getBuffer = pInstance->_TrustZoneService::DecryptN(*appInfo, pBuf); + return getBuffer; +} } Bundle::Bundle(WKBundleRef bundle) : @@ -889,10 +922,7 @@ std::string Bundle::DecryptResource(std::string resource, int size) struct stat buf; if (0 == stat(filePath.c_str(), &buf)) { const std::size_t fileSize = buf.st_size; - const std::size_t readBufSize = (fileSize > FILE_BUF_MAX_SIZE - ? FILE_BUF_MAX_SIZE : fileSize); - - std::unique_ptr inChunk(new unsigned char[readBufSize]); + std::unique_ptr inChunk; FILE* fp = fopen(filePath.c_str(), "rb"); if (NULL == fp) { @@ -902,22 +932,38 @@ std::string Bundle::DecryptResource(std::string resource, int size) std::unique_ptr DecryptedString(new unsigned char[fileSize]); - int count = 0; + std::string pkgid(DPL::ToUTF8String(m_widgetTizenId)); + + using namespace Tizen::Base; + const byte *b_pkgid = reinterpret_cast(pkgid.c_str()); + ByteBuffer appInfo; + appInfo.Construct(pkgid.length()); + appInfo.SetArray(b_pkgid, 0, pkgid.length()); + appInfo.Flip(); + int writeCount = 0; do { - size_t readSize = fread(inChunk.get(), sizeof(unsigned char), - readBufSize, fp); + unsigned char getDecSize[4]; + memset(getDecSize, 0x00, sizeof(getDecSize)); + size_t readSize = fread(getDecSize, sizeof(unsigned char), + sizeof(getDecSize), fp); if (0 != readSize) { - LogDebug("resource is encrypted. decrypting...."); - using namespace Tizen::Base; - ByteBuffer *getBuffer = - reinterpret_cast(DecryptChunkByTrustZone(inChunk.get(), - readSize)); - memcpy(DecryptedString.get() + (PLAIN_CHUNK_SIZE * count++), - getBuffer->GetPointer(), - (readSize < PLAIN_CHUNK_SIZE ? readSize : PLAIN_CHUNK_SIZE)); - getBuffer->Reset(); + int readBufSize = 0; + std::istringstream(std::string((char*)getDecSize)) >> readBufSize; + inChunk.reset(new unsigned char[readBufSize]); + + size_t decReadSize = fread(inChunk.get(), sizeof(unsigned char), + readBufSize, fp); + if (0 != decReadSize) { + ByteBuffer *getBuffer = + DecryptChunkByTrustZone(&appInfo, inChunk.get(), + decReadSize); + memcpy(DecryptedString.get() + writeCount, + getBuffer->GetPointer(), getBuffer->GetRemaining()); + writeCount += getBuffer->GetRemaining(); + getBuffer->Reset(); + } } } while( 0 == std::feof(fp)); @@ -943,31 +989,6 @@ std::string Bundle::DecryptResource(std::string resource, int size) return std::string(); } -void *Bundle::DecryptChunkByTrustZone(const unsigned char *inBuffer, - int inBufSize) -{ - using namespace Tizen::Base; - std::string pkgid(DPL::ToUTF8String(m_widgetTizenId)); - - const byte *b_pkgid = reinterpret_cast(pkgid.c_str()); - ByteBuffer appInfo; - appInfo.Construct(pkgid.length()); - appInfo.SetArray(b_pkgid, 0, pkgid.length()); - appInfo.Flip(); - - Tizen::Security::Crypto::_TrustZoneService* pInstance; - pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance(); - - ByteBuffer pBuf; - pBuf.Construct(inBufSize); - const byte *pByte = reinterpret_cast(inBuffer); - pBuf.SetArray(pByte, 0, inBufSize); - pBuf.Flip(); - - ByteBuffer *getBuffer = pInstance->_TrustZoneService::DecryptN(appInfo, pBuf); - return reinterpret_cast(getBuffer); -} - extern "C" { WK_EXPORT diff --git a/src/view/webkit/bundles/wrt-wk2-bundle.h b/src/view/webkit/bundles/wrt-wk2-bundle.h index 9d93fca..920c640 100644 --- a/src/view/webkit/bundles/wrt-wk2-bundle.h +++ b/src/view/webkit/bundles/wrt-wk2-bundle.h @@ -160,8 +160,6 @@ class Bundle void fixWKMessageArgs(std::string & argScale, std::string & argEncodedBundle, std::string & argTheme); - void *DecryptChunkByTrustZone(const unsigned char *inBuffer, - int inBufSize); }; #endif /* WRT_SRC_VIEW_WEBKIT2_WRT_WK2_BUNDLE_H_ */ -- 2.7.4