Fixed encryption logic
authorSoyoung Kim <sy037.kim@samsung.com>
Fri, 15 Mar 2013 08:24:38 +0000 (17:24 +0900)
committerSoyoung Kim <sy037.kim@samsung.com>
Fri, 15 Mar 2013 08:24:38 +0000 (17:24 +0900)
[Issue#] N/A
[Problem] occur crash during ecryption.
[Cause] should initialize osp app framework for trustzone api.
[Solution] call InitWebAppInfo() before using encryption.
[SCMRequest] N/A

src/jobs/widget_install/task_certify.cpp
src/jobs/widget_install/task_encrypt_resource.cpp

index cd38d74..c806945 100644 (file)
@@ -522,14 +522,7 @@ void TaskCertify::stepVerifyUpdate()
                     between old widget and installing widget");
         }
     } else {
-        if (NULL == newCertificate.Get() )
-            LogDebug("@@@@ new certificate nul");
-
-        if (NULL == oldCertificate.Get() )
-            LogDebug("@@@@ old certificate nul");
-
         if (!(NULL == newCertificate.Get() && NULL == oldCertificate.Get())) {
-        LogDebug("@@@@");
             ThrowMsg(Exceptions::InvalidPackage,
                     "Author signer certificates doesn't match \
                     between old widget and installing widget");
index 3de2de7..b88c27f 100644 (file)
@@ -29,6 +29,8 @@
 #include <string.h>
 #include <errno.h>
 #include <cstdio>
+#include <sstream>
+#include<iostream>
 
 #include <memory>
 
 
 using namespace WrtDB;
 
+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()
 {
@@ -168,6 +177,15 @@ Tizen::Base::ByteBuffer* EncryptChunkByTrustZone(
         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();
@@ -181,6 +199,27 @@ Tizen::Base::ByteBuffer* EncryptChunkByTrustZone(
     ByteBuffer* getBuffer = pInstance->_TrustZoneService::EncryptN(*appInfo, pBuf);
     return getBuffer;
 }
+
+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);
+    pBuf.SetArray(pByte, 0, pBufSize);
+    pBuf.Flip();
+
+    ByteBuffer* getBufferTEST =
+        pInstance->_TrustZoneService::DecryptN(*appInfo, pBuf);
+    return getBufferTEST;
+}
 }
 
 namespace Jobs {
@@ -299,7 +338,6 @@ void TaskEncryptResource::EncryptFile(const std::string &fileName)
         {
             bytesRead = readBytes(inChunk.get(), chunkSize, inFile.Get());
             if (0 != bytesRead) {
-
                 ByteBuffer *getBuffer = EncryptChunkByTrustZone(
                         &appInfo,
                         inChunk.get(), bytesRead);
@@ -309,16 +347,23 @@ void TaskEncryptResource::EncryptFile(const std::string &fileName)
                 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());
             }
 
         } while (0 == std::feof(inFile.Get()));
 
-        LogDebug("File encrypted successfully");
-
         outFile.Reset();
         inFile.Reset();
 
+        LogDebug("File encrypted successfully");
         LogDebug("Remove plain-text file: " << fileName);
         if (0 != unlink(fileName.c_str()))
         {