Change decryption API.
authorSoyoung Kim <sy037.kim@samsung.com>
Wed, 6 Mar 2013 12:11:40 +0000 (21:11 +0900)
committerGerrit Code Review <gerrit2@kim11>
Fri, 8 Mar 2013 08:01:16 +0000 (17:01 +0900)
[Issue#] N/A
[Problem] N/A
[Cause] N/A
[Solution] decryption API changed to using trust zone.
so to decryption resource should be changed.
[SCMRequest] his commit should be release with wrt-commons.

src/view/webkit/bundles/wrt-wk2-bundle.cpp

index 44b173a..05fc92d 100644 (file)
@@ -88,6 +88,8 @@ const char * const HTML_MIME = "text/html";
 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
 }
 
 Bundle::Bundle(WKBundleRef bundle) :
@@ -868,9 +870,13 @@ std::string Bundle::DecryptResource(std::string resource, int size)
             WrtDB::WidgetDAOReadOnly(m_widgetTizenId).
                 getEncryptedFileList(m_encryptedFiles);
         }
-        size_t bufSize = buf.st_size;
-        unsigned char contents[bufSize];
-        memset(contents, 0, bufSize);
+
+        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<unsigned char[]> inChunk(new unsigned char[readBufSize]);
+        std::unique_ptr<unsigned char[]> outChunk;
 
         FILE* fp = fopen(filePath.c_str(), "rb");
         if (NULL == fp) {
@@ -878,21 +884,30 @@ std::string Bundle::DecryptResource(std::string resource, int size)
             return std::string();
         }
 
-        size_t ret = fread(contents, sizeof(unsigned char), buf.st_size, fp);
-        if (ret < static_cast<size_t>(buf.st_size) ) {
-            LogDebug("Couldnot read file : " << filePath);
-            fclose(fp);
-            return std::string();
-        }
+        std::unique_ptr<unsigned char[]> DecryptedString(new unsigned
+                char[fileSize]);
+        int count = 0;
+
+        do {
+            size_t readSize = fread(inChunk.get(), sizeof(unsigned char),
+                    readBufSize, fp);
+
+            if (0 != readSize) {
+                LogDebug("resource is encrypted. decrypting....");
+                int decSize = m_resDec->DecryptChunkByTrustZone(
+                        DPL::ToUTF8String(m_widgetTizenId), inChunk.get(),
+                        readSize);
+                outChunk.reset(new unsigned char[decSize]);
+                m_resDec->getDecryptStringByTrustZone(outChunk.get());
+                memcpy(DecryptedString.get() + (PLAIN_CHUNK_SIZE * count++),
+                        outChunk.get(),
+                        (readSize < PLAIN_CHUNK_SIZE ? readSize : PLAIN_CHUNK_SIZE));
+            }
 
+        } while( 0 == std::feof(fp));
         fclose(fp);
 
-        LogDebug("resource is encrypted. decrypting....");
-        unsigned char outDecBuf[bufSize];
-        memset(outDecBuf, 0, bufSize);
-        m_resDec->GetDecryptedChunk(contents, outDecBuf, bufSize);
-        memset(outDecBuf + size, '\n', bufSize - size);
-
+        memset(DecryptedString.get() + size, '\n', fileSize - size);
         LogDebug("resource need to encoding base64");
         BIO *bmem, *b64;
         BUF_MEM *bptr;
@@ -900,7 +915,7 @@ std::string Bundle::DecryptResource(std::string resource, int size)
         b64 = BIO_new(BIO_f_base64());
         bmem = BIO_new(BIO_s_mem());
         b64 = BIO_push(b64, bmem);
-        BIO_write(b64, outDecBuf, bufSize);
+        BIO_write(b64, DecryptedString.get(), fileSize);
         BIO_flush(b64);
         BIO_get_mem_ptr(b64, &bptr);