2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FSec_TrustZoneService.cpp
19 * @brief This is the implementation for the _AccessController class.
22 #include <unique_ptr.h>
23 #include <FBaseSysLog.h>
24 #include <FBaseString.h>
25 #include <FBaseColArrayList.h>
27 #include <FIo_IpcClient.h>
29 #include <FIo_IpcCommonParamTraits.h>
30 #include <FSecCryptoAesCipher.h>
31 #include "FSecCrypto_TrustZoneService.h"
32 #include "FSecCrypto_TrustZoneServiceMessage.h"
35 using namespace Tizen::Io;
36 using namespace Tizen::Base;
37 using namespace Tizen::Security;
39 namespace Tizen { namespace Security { namespace Crypto
42 _TrustZoneService* _TrustZoneService::__pTrustZoneService = null;
43 _IpcClient* _TrustZoneService::__pIpcClient = null;
44 unsigned int _TrustZoneService::__refCount = 0;
46 _TrustZoneService::_TrustZoneService(void)
51 _TrustZoneService::~_TrustZoneService(void)
57 _TrustZoneService::GetInstance(void)
59 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
60 if(!__pTrustZoneService)
62 pthread_once(&once_block, Initialize);
65 if(!__pIpcClient && __refCount == 0)
67 std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
68 SysTryReturn(NID_SEC_CRYPTO, pIpcClient != null, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
70 __pIpcClient = pIpcClient.release();
72 result r = __pIpcClient->Construct("osp.security.ipcserver.trustzoneservice");
73 SysTryReturn(NID_SEC_CRYPTO, r == E_SUCCESS, null, E_SYSTEM, "Failed to construct the instance of IPC.");
77 return __pTrustZoneService;
81 _TrustZoneService::EncryptN(const ByteBuffer& appInfo, const ByteBuffer& plainBuffer)
84 result ret = E_SUCCESS;
85 std::unique_ptr<IPC::Message> pMessage(null);
86 ByteBuffer* pEncryptedBuffer = null;
90 ipcBuffer.pBuffer = null;
92 pMessage.reset(new (std::nothrow) TrustZoneService_Encrypt(appInfo, plainBuffer, &ipcBuffer, &ret));
93 TryReturnResult(pMessage != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
95 result r = __pIpcClient->SendRequest(pMessage.get());
96 TryReturnResult(r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to send IPC message.");
97 TryReturnResult(ret == E_SUCCESS, null, ret, "[%s] Failed to encrypt", GetErrorMessage(ret));
99 byte* pBuffer = static_cast<byte*>(ipcBuffer.pBuffer);
100 TryReturnResult(pBuffer != null && ipcBuffer.size != 0, null, E_SYSTEM, "[E_SYSTEM] Failed to get encrypted data(%d).", ipcBuffer.size);
102 pEncryptedBuffer = new (std::nothrow) ByteBuffer();
103 TryReturnResult(pEncryptedBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
105 r = pEncryptedBuffer->Construct(ipcBuffer.size);
106 r = pEncryptedBuffer->SetArray(pBuffer, 0, ipcBuffer.size);
107 TryReturnResult(r == E_SUCCESS, null, r, "[%s] propagating.", r);
108 pEncryptedBuffer->Flip();
112 return pEncryptedBuffer;
116 _TrustZoneService::DecryptN(const ByteBuffer& appInfo, const ByteBuffer& encryptedBuffer)
119 result ret = E_SUCCESS;
120 std::unique_ptr<IPC::Message> pMessage(null);
121 ByteBuffer* pPlainBuffer = null;
123 _IpcBuffer ipcBuffer;
125 ipcBuffer.pBuffer = null;
127 pMessage.reset(new (std::nothrow) TrustZoneService_Decrypt(appInfo, encryptedBuffer, &ipcBuffer, &ret));
128 TryReturnResult(pMessage != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
130 result r = __pIpcClient->SendRequest(pMessage.get());
131 TryReturnResult(r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to send IPC message.");
132 TryReturnResult(ret == E_SUCCESS, null, ret, "[%s] Failed to Decrypt", GetErrorMessage(ret));
134 byte* pBuffer = static_cast<byte*>(ipcBuffer.pBuffer);
135 TryReturnResult(pBuffer != null && ipcBuffer.size != 0, null, E_SYSTEM, "[E_SYSTEM] Failed to Decrtype");
137 pPlainBuffer = new (std::nothrow) ByteBuffer();
138 TryReturnResult(pPlainBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
140 r = pPlainBuffer->Construct(ipcBuffer.size);
141 r = pPlainBuffer->SetArray(pBuffer, 0, ipcBuffer.size);
142 TryReturnResult(r == E_SUCCESS, null, r, "[%s] propagating.", r);
143 pPlainBuffer->Flip();
152 _TrustZoneService::Initialize(void)
155 static _TrustZoneService trustZoneService;
156 _TrustZoneService::__pTrustZoneService = &trustZoneService;
161 _TrustZoneService::Release()
163 if(--(__refCount) == 0)
165 SysLog(NID_SEC_CRYPTO, "Close IPC connection");
171 }}} // Tizen::Security::Crypto