Fix prevent issues in FSecurity
[platform/framework/native/appfw.git] / src / security / FSec_DeviceKeyGenerator.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 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_DeviceKeyGenerator.cpp
20  * @brief       This file contains the implementation of DeviceKeyGenerator class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FBaseResult.h>
25 #include <FBaseErrors.h>
26 #include <FBaseUtilStringUtil.h>
27 #include <FSecCryptoSha1Hash.h>
28 #include <FSecCryptoSha1Hmac.h>
29 #include <FSecSecretKey.h>
30 #include <FBaseSysLog.h>
31 #include <FBase_StringConverter.h>
32 #include <FSec_DeviceKeyGenerator.h>
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Utility;
36 using namespace Tizen::Security::Crypto;
37
38
39 namespace Tizen { namespace Security
40 {
41
42 static const int _HASH_LEN = 20;
43
44 _DeviceKeyGenerator::_DeviceKeyGenerator(void)
45 {
46
47 }
48
49 _DeviceKeyGenerator::~_DeviceKeyGenerator(void)
50 {
51
52 }
53
54 ISecretKey*
55 _DeviceKeyGenerator::GenerateDeviceKeyN(int keySize)
56 {
57         result r = E_SUCCESS;
58         String deviceInfo;
59         ByteBuffer deviceInfoBuffer;
60         std::unique_ptr <ByteBuffer> pHashValue(null);
61         std::unique_ptr <ISecretKey> pKey(null);
62         ByteBuffer* pTempValue = null;
63         ByteBuffer* pTempInfoBuffer = null;
64         Sha1Hash hash;
65         int count = 0;
66         char* pDeviceInfo = null;
67
68         SysLog(NID_SEC, "GenerateDeviceKeyN called.");
69
70         SysTryReturn(NID_SEC, keySize > 0, null, E_INVALID_ARG,
71                                 "[E_INVALID_ARG] The device key size MUST be a valid integer greater than 0.");
72         deviceInfo.Append(L"1234567890abcdefghijklmnopqrstuvwxyz");
73         if (keySize % _HASH_LEN == 0)
74         {
75                 count = keySize / _HASH_LEN;
76         }
77         else
78         {
79                 count = keySize / _HASH_LEN + 1;
80         }
81
82         pHashValue.reset(new (std::nothrow) ByteBuffer());
83         SysTryReturn(NID_SEC, pHashValue != null, null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
84         r = pHashValue->Construct(count * _HASH_LEN);
85         SysTryReturn(NID_SEC, r == E_SUCCESS, null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
86
87         pDeviceInfo = _StringConverter::CopyToCharArrayN(deviceInfo);
88         deviceInfoBuffer.Construct(deviceInfo.GetLength());
89         r = deviceInfoBuffer.SetArray(reinterpret_cast <byte*>(pDeviceInfo), 0, deviceInfo.GetLength());
90         SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
91         deviceInfoBuffer.Flip();
92         delete[] pDeviceInfo;
93
94         for (int i = 0; i < count; i++)
95         {
96                 if (i == 0)
97                 {
98                         pTempValue = hash.GetHashN(deviceInfoBuffer);
99                 }
100                 else
101                 {
102                         pTempValue = hash.GetHashN(*pTempInfoBuffer);
103                 }
104                 SysTryCatch(NID_SEC, pTempValue != null, , GetLastResult(), "[%s] Failed to generate hash code.",
105                                    GetErrorMessage(GetLastResult()));
106
107                 if (pTempInfoBuffer != null)
108                         delete pTempInfoBuffer;
109                 pTempInfoBuffer = new (std::nothrow) ByteBuffer();
110                 r = pTempInfoBuffer->Construct(*pTempValue);
111                 delete pTempValue;
112                 SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
113                 r = pHashValue->CopyFrom(*pTempInfoBuffer);
114                 SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
115         }
116
117         pHashValue->Flip();
118         pHashValue->SetLimit(keySize);
119         pKey.reset(new (std::nothrow) SecretKey());
120         SysTryCatch(NID_SEC, pKey != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
121         r = pKey->SetKey(*(pHashValue.get()));
122         SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] Failed to generate device unique key.", GetErrorMessage(r));
123
124 CATCH:
125         SetLastResult(r);
126         delete pTempInfoBuffer;
127
128         return pKey.release();
129 }
130
131 ISecretKey*
132 _DeviceKeyGenerator::GenerateDeviceKeyN(String& appId, int keySize)
133 {
134         result r = E_SUCCESS;
135         String deviceInfo;
136         ByteBuffer deviceInfoBuffer;
137         std::unique_ptr <ByteBuffer> pHashValue(null);
138         std::unique_ptr <ByteBuffer> pHmacKey(null);
139         std::unique_ptr <ISecretKey> pSecretKey(null);
140         std::unique_ptr <ISecretKey> pKey(null);
141         ByteBuffer* pTempValue = null;
142         ByteBuffer* pTempInfoBuffer = null;
143         Sha1Hmac hmac;
144         int count = 0;
145         char* pDeviceInfo = null;
146
147         SysLog(NID_SEC, "GenerateDeviceKeyN called.");
148
149         SysTryReturn(NID_SEC, keySize > 0 && appId.GetLength() > 0, null, E_INVALID_ARG,
150                                 "[E_INVALID_ARG] The device key size MUST be a valid integer greater than 0.");
151
152 //ToDo
153 //Add slp API
154
155         deviceInfo.Append(L"1234567890abcdefghijklmnopqrstuvwxyz");
156         if (keySize % _HASH_LEN == 0)
157         {
158                 count = keySize / _HASH_LEN;
159         }
160         else
161         {
162                 count = keySize / _HASH_LEN + 1;
163         }
164
165         pHashValue.reset(new (std::nothrow) ByteBuffer());
166         SysTryReturn(NID_SEC, pHashValue != null, null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
167         r = pHashValue->Construct(count * _HASH_LEN);
168         SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
169
170         pDeviceInfo = _StringConverter::CopyToCharArrayN(deviceInfo);
171         deviceInfoBuffer.Construct(deviceInfo.GetLength());
172         r = deviceInfoBuffer.SetArray(reinterpret_cast <byte*>(pDeviceInfo), 0, deviceInfo.GetLength());
173         SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
174         deviceInfoBuffer.Flip();
175         delete[] pDeviceInfo;
176
177         pHmacKey.reset(StringUtil::StringToUtf8N(appId));
178
179         pSecretKey.reset(new (std::nothrow) SecretKey());
180         r = pSecretKey->SetKey(*(pHmacKey.get()));
181         SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
182         hmac.SetKey(*(pSecretKey.get()));
183
184         for (int i = 0; i < count; i++)
185         {
186                 if (i == 0)
187                 {
188                         pTempValue = hmac.GetHmacN(deviceInfoBuffer);
189                 }
190                 else
191                 {
192                         pTempValue = hmac.GetHmacN(*pTempInfoBuffer);
193                 }
194                 SysTryCatch(NID_SEC, pTempValue != null, , GetLastResult(), "[%s] Failed to generate hash code.",
195                                    GetErrorMessage(GetLastResult()));
196
197                 if (pTempInfoBuffer != null)
198                         delete pTempInfoBuffer;
199                 pTempInfoBuffer = new (std::nothrow) ByteBuffer();
200                 r = pTempInfoBuffer->Construct(*pTempValue);
201                 delete pTempValue;
202                 SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
203                 r = pHashValue->CopyFrom(*pTempInfoBuffer);
204                 SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] A system error has occurred.", GetErrorMessage(r));
205         }
206
207         pHashValue->Flip();
208         pHashValue->SetLimit(keySize);
209         pKey.reset(new (std::nothrow) SecretKey());
210         SysTryCatch(NID_SEC, pKey != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
211         r = pKey->SetKey(*(pHashValue.get()));
212         SysTryCatch(NID_SEC, r == E_SUCCESS, , r, "[%s] Failed to generate device unique key.", GetErrorMessage(r));
213
214 CATCH:
215         SetLastResult(r);
216         delete pTempInfoBuffer;
217
218         return pKey.release();
219 }
220
221 } //Tizen::Security
222 } //Osp