Update change log and spec for wrt-plugins-tizen_0.4.12
[framework/web/wrt-plugins-tizen.git] / src / SecureStorage / SecureStorageManager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "SecureStorageManager.h"
18 #include <ss_manager.h>
19 #include <cassert>
20 #include <exception>
21 #include <Commons/Exception.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <dpl/scoped_array.h>
24
25 using namespace DPL;
26 using namespace WrtDeviceApis::Commons;
27
28 namespace TizenApis {
29 namespace Tizen1_0{
30         
31 std::string SecureStorageManager::m_filePath = "securedata.txt";
32 int SecureStorageManager::m_maxLength = 256;
33
34 SecureStorageManager::SecureStorageManager()
35 {
36         LogDebug("OK");
37
38         try 
39         {
40                 loadSecurePairData();
41         }
42         catch (const WrtDeviceApis::Commons::Exception& ex) 
43         {
44                 LogError("Exception: " << ex.GetMessage());
45                 LogDebug("there is no file");
46         }
47
48         LogDebug("initial map size : " << m_securePairData.size());
49 }
50
51 SecureStorageManager::~SecureStorageManager()
52 {
53         LogDebug("OK");
54 }
55
56 std::string SecureStorageManager::get(std::string key)
57 {
58         LogDebug("OK");
59
60         std::map<std::string,std::string>::iterator it = m_securePairData.find(key);    
61
62         if (it == m_securePairData.end())
63         {
64                 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "not found key");
65         }
66         
67         return m_securePairData[key];
68 }
69
70 std::vector<std::string> SecureStorageManager::listKeys()
71 {
72         std::vector<std::string> vector;
73         std::map<std::string,std::string>::iterator it; 
74
75         LogDebug("OK");
76
77         for (it = m_securePairData.begin(); it != m_securePairData.end(); ++it)
78         {
79                 vector.push_back((*it).first);
80         }
81         
82         return vector;
83 }
84 void SecureStorageManager::set(std::string key, std::string value)
85 {
86         m_securePairData[key] = value;
87
88         LogDebug("Save current data");
89         saveSecurePairData();
90 }
91
92 void SecureStorageManager::remove(std::string key)
93 {
94         LogDebug("OK");
95
96         std::map<std::string,std::string>::iterator it = m_securePairData.find(key);    
97
98         if (it == m_securePairData.end())
99         {
100                 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "not found key");
101         }
102         m_securePairData.erase(it);
103         LogDebug("Save current data");
104         saveSecurePairData();
105 }
106
107 void SecureStorageManager::removeAll()
108 {
109         LogDebug("OK");
110
111         ssm_flag flag = SSM_FLAG_SECRET_OPERATION;
112                 
113         m_securePairData.clear();
114         ssm_delete_file(m_filePath.c_str(), flag, NULL);
115 }
116
117 void SecureStorageManager::saveSecurePairData()
118 {
119         LogDebug("OK");
120
121         std::map<std::string,std::string>::iterator it; 
122         size_t dataSize = 0;
123         size_t index = 0;
124         size_t num = 0;
125         size_t mapSize = m_securePairData.size();
126         int ret = -1;   
127
128         for (it = m_securePairData.begin(); it != m_securePairData.end(); ++it)
129         {
130                 dataSize += (*it).first.size();
131                 dataSize += (*it).second.size();
132         }
133         
134         dataSize += sizeof(size_t) * 2 * m_securePairData.size();
135         dataSize += sizeof(size_t) * 2;
136
137         DPL::ScopedArray<char> saveBuf(new char[dataSize]);
138
139         memcpy(&saveBuf[index], (char*)&mapSize, sizeof(size_t));
140         index += sizeof(size_t);
141
142         memcpy(&saveBuf[index], (char*)&dataSize, sizeof(size_t));
143         index += sizeof(size_t);
144         
145         for (it = m_securePairData.begin(); it != m_securePairData.end(); ++it)
146         {
147                 num = (*it).first.size();
148                 memcpy(&saveBuf[index], (char*)&num, sizeof(size_t));
149                 index += sizeof(size_t);
150
151                 memcpy(&saveBuf[index], (char*)(*it).first.c_str(), (*it).first.size());
152                 index += (*it).first.size();                    
153
154                 num = (*it).second.size();              
155                 memcpy(&saveBuf[index], (char*)&num, sizeof(size_t));
156                 index += sizeof(size_t);
157                 memcpy(&saveBuf[index], (char*)(*it).second.c_str(), (*it).second.size());
158                 index += (*it).second.size();                   
159         }       
160
161         ssm_flag flag = SSM_FLAG_SECRET_OPERATION;
162
163         ret = ssm_write_buffer(saveBuf.Get(), dataSize, m_filePath.c_str(), flag, NULL);
164
165         if (ret < 0) 
166         {
167                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ssm write buf error");
168         }
169         LogDebug("save result : " << ret);
170 }
171
172 std::string SecureStorageManager::readStringFromMemory(const char *saveBuf, size_t &current, size_t dataSize) 
173 {
174         std::string value = "";
175         size_t num = 0;
176         char tempbuf[m_maxLength];
177
178         if (current + sizeof(size_t) > dataSize)
179         {
180                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get size error ");
181         }
182         
183         memcpy(&num, &saveBuf[current], sizeof(size_t));
184         memset(tempbuf, 0, sizeof(tempbuf));
185         current += sizeof(size_t);      
186
187         if (current + num > dataSize)
188         {
189                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get size error ");
190         }               
191         
192         memcpy(tempbuf, &saveBuf[current], num);
193         current += num; 
194         value = tempbuf;        
195         
196         return value;
197 }
198
199
200 void SecureStorageManager::loadSecurePairData()
201 {
202         LogDebug("OK");
203         
204         int ret = -1;   
205         size_t num = 0;
206         size_t dataSize = 0;
207         size_t i = 0;   
208         size_t readlen = 0;     
209         char* retbuf = NULL;    
210         ssm_file_info_t sfi;
211         ssm_flag flag = SSM_FLAG_SECRET_OPERATION;
212         size_t index = 0;
213
214         m_securePairData.clear();
215         ret = ssm_getinfo(m_filePath.c_str(), &sfi, flag, NULL);
216
217         if (ret < 0)
218         {
219                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get info error");
220         }
221
222         if (sfi.originSize == 0) 
223         {
224                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "originSize error");
225         }
226         
227         DPL::ScopedArray<char> saveBuf(new char[sfi.originSize]);
228         retbuf = saveBuf.Get();
229
230         memset(retbuf, 0x00, sizeof(char) * sfi.originSize);
231         ret = ssm_read(m_filePath.c_str(), retbuf, sfi.originSize, &readlen, flag, NULL);
232
233         if (ret < 0)
234         {
235                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "read buf error");
236         }
237
238         LogDebug("read result : " << ret << ", length:" << readlen);
239
240         memcpy(&num, &saveBuf[index], sizeof(size_t));
241         index += sizeof(size_t);
242
243         memcpy(&dataSize, &saveBuf[index], sizeof(size_t));
244         index += sizeof(size_t);
245         
246         LogDebug("dataSize" <<  dataSize << "-" << num);
247
248         for (i = 0; i < num; i++) 
249         {
250                 std::string key = readStringFromMemory(retbuf, index, dataSize);
251                 std::string value = readStringFromMemory(retbuf, index, dataSize);
252
253                 if (key.size() == 0 || value.size() == 0) 
254                 {
255                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "read buf error");
256                 }
257                 m_securePairData[key] = value;
258                 LogDebug("contents:" << key << "-" << m_securePairData[key]);
259         }       
260 }
261
262
263 }
264 }
265