2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FSec_TrustZoneService.cpp
20 * @brief This is the implementation for the _AccessController class.
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseString.h>
26 #include <FBaseColArrayList.h>
28 #include <FIo_IpcClient.h>
30 #include <FIo_IpcCommonParamTraits.h>
31 #include <FSecCryptoAesCipher.h>
32 #include "FSecCrypto_TrustZoneService.h"
33 #include "FSecCrypto_TrustZoneServiceMessage.h"
36 using namespace Tizen::Io;
37 using namespace Tizen::Base;
38 using namespace Tizen::Security;
40 namespace Tizen { namespace Security { namespace Crypto
43 _TrustZoneService* _TrustZoneService::__pTrustZoneService = null;
44 _IpcClient* _TrustZoneService::__pIpcClient = null;
45 unsigned int _TrustZoneService::__refCount = 0;
47 _TrustZoneService::_TrustZoneService(void)
52 _TrustZoneService::~_TrustZoneService(void)
58 _TrustZoneService::GetInstance(void)
60 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
61 if(!__pTrustZoneService)
63 pthread_once(&once_block, Initialize);
66 if(!__pIpcClient && __refCount == 0)
68 std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
69 SysTryReturn(NID_SEC_CRYPTO, pIpcClient != null, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
71 __pIpcClient = pIpcClient.release();
73 result r = __pIpcClient->Construct("osp.security.ipcserver.trustzoneservice");
74 SysTryReturn(NID_SEC_CRYPTO, r == E_SUCCESS, null, E_SYSTEM, "Failed to construct the instance of IPC.");
78 return __pTrustZoneService;
82 _TrustZoneService::EncryptN(const ByteBuffer& appInfo, const ByteBuffer& plainBuffer)
85 result ret = E_SUCCESS;
86 std::unique_ptr<IPC::Message> pMessage(null);
87 ByteBuffer* pEncryptedBuffer = null;
91 ipcBuffer.pBuffer = null;
93 pMessage.reset(new (std::nothrow) TrustZoneService_Encrypt(appInfo, plainBuffer, &ipcBuffer, &ret));
94 TryReturnResult(pMessage != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
96 result r = __pIpcClient->SendRequest(pMessage.get());
97 TryReturnResult(r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to send IPC message.");
98 TryReturnResult(ret == E_SUCCESS, null, ret, "[%s] Failed to encrypt", GetErrorMessage(ret));
100 byte* pBuffer = static_cast<byte*>(ipcBuffer.pBuffer);
101 TryReturnResult(pBuffer != null && ipcBuffer.size != 0, null, E_SYSTEM, "[E_SYSTEM] Failed to get encrypted data(%d).", ipcBuffer.size);
103 pEncryptedBuffer = new (std::nothrow) ByteBuffer();
104 TryReturnResult(pEncryptedBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
106 r = pEncryptedBuffer->Construct(ipcBuffer.size);
107 r = pEncryptedBuffer->SetArray(pBuffer, 0, ipcBuffer.size);
108 TryReturnResult(r == E_SUCCESS, null, r, "[%s] propagating.", r);
109 pEncryptedBuffer->Flip();
113 return pEncryptedBuffer;
117 _TrustZoneService::DecryptN(const ByteBuffer& appInfo, const ByteBuffer& encryptedBuffer)
120 result ret = E_SUCCESS;
121 std::unique_ptr<IPC::Message> pMessage(null);
122 ByteBuffer* pPlainBuffer = null;
124 _IpcBuffer ipcBuffer;
126 ipcBuffer.pBuffer = null;
128 pMessage.reset(new (std::nothrow) TrustZoneService_Decrypt(appInfo, encryptedBuffer, &ipcBuffer, &ret));
129 TryReturnResult(pMessage != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
131 result r = __pIpcClient->SendRequest(pMessage.get());
132 TryReturnResult(r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to send IPC message.");
133 TryReturnResult(ret == E_SUCCESS, null, ret, "[%s] Failed to Decrypt", GetErrorMessage(ret));
135 byte* pBuffer = static_cast<byte*>(ipcBuffer.pBuffer);
136 TryReturnResult(pBuffer != null && ipcBuffer.size != 0, null, E_SYSTEM, "[E_SYSTEM] Failed to Decrtype");
138 pPlainBuffer = new (std::nothrow) ByteBuffer();
139 TryReturnResult(pPlainBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
141 r = pPlainBuffer->Construct(ipcBuffer.size);
142 r = pPlainBuffer->SetArray(pBuffer, 0, ipcBuffer.size);
143 TryReturnResult(r == E_SUCCESS, null, r, "[%s] propagating.", r);
144 pPlainBuffer->Flip();
153 _TrustZoneService::Initialize(void)
156 static _TrustZoneService trustZoneService;
157 _TrustZoneService::__pTrustZoneService = &trustZoneService;
162 _TrustZoneService::Release()
164 if(--(__refCount) == 0)
166 SysLog(NID_SEC_CRYPTO, "Close IPC connection");
172 }}} // Tizen::Security::Crypto