remove _DeviceKeyGenerator class
[platform/framework/native/appfw.git] / src / security / crypto / FSecCrypto_TrustZoneService.cpp
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FSec_TrustZoneService.cpp
19  * @brief       This is the implementation for the _AccessController class.
20  */
21
22 #include <unique_ptr.h>
23 #include <FBaseSysLog.h>
24 #include <FBaseString.h>
25 #include <FBaseColArrayList.h>
26 #include <FIoFile.h>
27 #include <FIo_IpcClient.h>
28 #include <FBase.h>
29 #include <FIo_IpcCommonParamTraits.h>
30 #include <FSecCryptoAesCipher.h>
31 #include "FSecCrypto_TrustZoneService.h"
32 #include "FSecCrypto_TrustZoneServiceMessage.h"
33
34
35 using namespace Tizen::Io;
36 using namespace Tizen::Base;
37 using namespace Tizen::Security;
38
39 namespace Tizen { namespace Security { namespace Crypto
40 {
41
42 _TrustZoneService* _TrustZoneService::__pTrustZoneService = null;
43 _IpcClient* _TrustZoneService::__pIpcClient = null;
44 unsigned int _TrustZoneService::__refCount = 0;
45
46 _TrustZoneService::_TrustZoneService(void)
47 {
48
49 }
50
51 _TrustZoneService::~_TrustZoneService(void)
52 {
53
54 }
55
56 _TrustZoneService*
57 _TrustZoneService::GetInstance(void)
58 {
59         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
60         if(!__pTrustZoneService)
61         {
62                 pthread_once(&once_block, Initialize);
63         }
64
65         if(!__pIpcClient && __refCount == 0)
66         {
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.");
69
70                 __pIpcClient = pIpcClient.release();
71
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.");
74         }
75         __refCount++;
76
77         return __pTrustZoneService;
78 }
79
80 ByteBuffer*
81 _TrustZoneService::EncryptN(const ByteBuffer& appInfo, const ByteBuffer& plainBuffer)
82 {
83         
84         result ret = E_SUCCESS;
85         std::unique_ptr<IPC::Message> pMessage(null);
86         ByteBuffer* pEncryptedBuffer = null;
87
88         _IpcBuffer ipcBuffer;
89         ipcBuffer.size = 0;
90         ipcBuffer.pBuffer = null;
91
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.");
94
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));
98
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);
101
102         pEncryptedBuffer = new (std::nothrow) ByteBuffer();
103         TryReturnResult(pEncryptedBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
104
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();
109
110         free(pBuffer); 
111
112         return pEncryptedBuffer;
113 }
114
115 ByteBuffer*
116 _TrustZoneService::DecryptN(const ByteBuffer& appInfo, const ByteBuffer& encryptedBuffer)
117 {
118         
119         result ret = E_SUCCESS;
120         std::unique_ptr<IPC::Message> pMessage(null);
121         ByteBuffer* pPlainBuffer = null;
122
123         _IpcBuffer ipcBuffer;
124         ipcBuffer.size = 0;
125         ipcBuffer.pBuffer = null;
126
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.");
129
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));
133
134         byte* pBuffer = static_cast<byte*>(ipcBuffer.pBuffer);
135         TryReturnResult(pBuffer != null && ipcBuffer.size != 0, null, E_SYSTEM, "[E_SYSTEM] Failed to Decrtype");
136
137         pPlainBuffer = new (std::nothrow) ByteBuffer();
138         TryReturnResult(pPlainBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
139
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();
144
145         free(pBuffer);
146
147         return pPlainBuffer;
148 }
149
150
151 void
152 _TrustZoneService::Initialize(void)
153 {
154
155         static _TrustZoneService trustZoneService;
156         _TrustZoneService::__pTrustZoneService = &trustZoneService;
157
158 }
159
160 void
161 _TrustZoneService::Release()
162 {
163         if(--(__refCount) == 0)
164         {
165                 SysLog(NID_SEC_CRYPTO, "Close IPC connection");
166                 delete __pIpcClient;
167                 __pIpcClient = null;
168         }
169 }
170
171 }}} // Tizen::Security::Crypto