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