2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "SecureStorageManager.h"
18 #include <ss_manager.h>
21 #include <Commons/Exception.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <dpl/scoped_array.h>
26 using namespace WrtDeviceApis::Commons;
31 std::string SecureStorageManager::m_filePath = "securedata.txt";
32 int SecureStorageManager::m_maxLength = 256;
34 SecureStorageManager::SecureStorageManager()
42 catch (const WrtDeviceApis::Commons::Exception& ex)
44 LoggerE("Exception: " << ex.GetMessage());
45 LoggerD("there is no file");
48 LoggerD("initial map size : " << m_securePairData.size());
51 SecureStorageManager::~SecureStorageManager()
56 std::string SecureStorageManager::get(std::string key)
60 std::map<std::string,std::string>::iterator it = m_securePairData.find(key);
62 if (it == m_securePairData.end())
64 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "not found key");
67 return m_securePairData[key];
70 std::vector<std::string> SecureStorageManager::listKeys()
72 std::vector<std::string> vector;
73 std::map<std::string,std::string>::iterator it;
77 for (it = m_securePairData.begin(); it != m_securePairData.end(); ++it)
79 vector.push_back((*it).first);
84 void SecureStorageManager::set(std::string key, std::string value)
86 m_securePairData[key] = value;
88 LoggerD("Save current data");
92 void SecureStorageManager::remove(std::string key)
96 std::map<std::string,std::string>::iterator it = m_securePairData.find(key);
98 if (it == m_securePairData.end())
100 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "not found key");
102 m_securePairData.erase(it);
103 LoggerD("Save current data");
104 saveSecurePairData();
107 void SecureStorageManager::removeAll()
111 ssm_flag flag = SSM_FLAG_SECRET_OPERATION;
113 m_securePairData.clear();
114 ssm_delete_file(m_filePath.c_str(), flag, NULL);
117 void SecureStorageManager::saveSecurePairData()
121 std::map<std::string,std::string>::iterator it;
125 size_t mapSize = m_securePairData.size();
128 for (it = m_securePairData.begin(); it != m_securePairData.end(); ++it)
130 dataSize += (*it).first.size();
131 dataSize += (*it).second.size();
134 dataSize += sizeof(size_t) * 2 * m_securePairData.size();
135 dataSize += sizeof(size_t) * 2;
137 DPL::ScopedArray<char> saveBuf(new char[dataSize]);
139 memcpy(&saveBuf[index], (char*)&mapSize, sizeof(size_t));
140 index += sizeof(size_t);
142 memcpy(&saveBuf[index], (char*)&dataSize, sizeof(size_t));
143 index += sizeof(size_t);
145 for (it = m_securePairData.begin(); it != m_securePairData.end(); ++it)
147 num = (*it).first.size();
148 memcpy(&saveBuf[index], (char*)&num, sizeof(size_t));
149 index += sizeof(size_t);
151 memcpy(&saveBuf[index], (char*)(*it).first.c_str(), (*it).first.size());
152 index += (*it).first.size();
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();
161 ssm_flag flag = SSM_FLAG_SECRET_OPERATION;
163 ret = ssm_write_buffer(saveBuf.Get(), dataSize, m_filePath.c_str(), flag, NULL);
167 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ssm write buf error");
169 LoggerD("save result : " << ret);
172 std::string SecureStorageManager::readStringFromMemory(const char *saveBuf, size_t ¤t, size_t dataSize)
174 std::string value = "";
176 char tempbuf[m_maxLength];
178 if (current + sizeof(size_t) > dataSize)
180 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get size error ");
183 memcpy(&num, &saveBuf[current], sizeof(size_t));
184 memset(tempbuf, 0, sizeof(tempbuf));
185 current += sizeof(size_t);
187 if (current + num > dataSize)
189 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get size error ");
192 memcpy(tempbuf, &saveBuf[current], num);
200 void SecureStorageManager::loadSecurePairData()
211 ssm_flag flag = SSM_FLAG_SECRET_OPERATION;
214 m_securePairData.clear();
215 ret = ssm_getinfo(m_filePath.c_str(), &sfi, flag, NULL);
219 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get info error");
222 if (sfi.originSize == 0)
224 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "originSize error");
227 DPL::ScopedArray<char> saveBuf(new char[sfi.originSize]);
228 retbuf = saveBuf.Get();
230 memset(retbuf, 0x00, sizeof(char) * sfi.originSize);
231 ret = ssm_read(m_filePath.c_str(), retbuf, sfi.originSize, &readlen, flag, NULL);
235 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "read buf error");
238 LoggerD("read result : " << ret << ", length:" << readlen);
240 memcpy(&num, &saveBuf[index], sizeof(size_t));
241 index += sizeof(size_t);
243 memcpy(&dataSize, &saveBuf[index], sizeof(size_t));
244 index += sizeof(size_t);
246 LoggerD("dataSize" << dataSize << "-" << num);
248 for (i = 0; i < num; i++)
250 std::string key = readStringFromMemory(retbuf, index, dataSize);
251 std::string value = readStringFromMemory(retbuf, index, dataSize);
253 if (key.size() == 0 || value.size() == 0)
255 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "read buf error");
257 m_securePairData[key] = value;
258 LoggerD("contents:" << key << "-" << m_securePairData[key]);