2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FSec_PrivilegeManager.cpp
20 * @brief This is the implementation for the _PrivilegeManager class.
25 #include <unique_ptr.h>
26 #include <FAppPkg_PackageInfoImpl.h>
27 #include <FAppPkg_PackageManagerImpl.h>
28 #include <FBaseString.h>
29 #include <FBaseSysLog.h>
30 #include <FIo_IpcClient.h>
31 #include "FSec_AccessControlTypes.h"
32 #include "FSec_PrivilegeManager.h"
33 #include "FSec_PrivilegeManagerMessage.h"
34 #include "FSec_PrivilegeCache.h"
35 #include "FSec_PrivilegeInfo.h"
37 using namespace Tizen::App;
38 using namespace Tizen::App::Package;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Io;
43 namespace Tizen { namespace Security
46 _PrivilegeManager* _PrivilegeManager::__pPrivilegeManagerInstance = null;
48 _PrivilegeManager::_PrivilegeManager(void)
49 : __pPrivilegeCache(null)
54 _PrivilegeManager::~_PrivilegeManager(void)
56 delete __pPrivilegeCache;
61 _PrivilegeManager::Construct(void)
65 _PackageManagerImpl* pPackageManagerImpl = null;
67 pPackageManagerImpl = _PackageManagerImpl::GetInstance();
68 SysTryReturnResult(NID_SEC, pPackageManagerImpl != null, E_SYSTEM, "An unexpected system error occurred.");
70 //pPackageManagerImpl->RemoveEventListener(this);
71 r = pPackageManagerImpl->AddEventListener(this);
72 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
74 __pPrivilegeCache = new (std::nothrow) _PrivilegeCache();
75 SysTryReturnResult(NID_SEC, __pPrivilegeCache != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
77 r = __pPrivilegeCache->Construct();
78 SysTryCatch(NID_SEC, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
84 delete __pPrivilegeCache;
89 _PrivilegeManager::InitInstance(void)
92 static _PrivilegeManager instance;
96 r = instance.Construct();
97 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
99 __pPrivilegeManagerInstance = &instance;
105 _PrivilegeManager::GetInstance(void)
107 result r = E_SUCCESS;
108 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
112 if (__pPrivilegeManagerInstance == null)
114 pthread_once(&onceBlock, InitInstance);
119 onceBlock = PTHREAD_ONCE_INIT;
123 return __pPrivilegeManagerInstance;
128 _PrivilegeManager::GetPrivilegeListN(const AppId& appId)
130 result r = E_SUCCESS;
131 _PrivilegeInfo privilegeInfo;
132 byte* pTargetBitwisePrivilege = null;
133 IList* pReturnPrivilegeList = null;
134 String encryptedPrivileges;
136 ArrayList* pPrivilegeList = null;
140 r = RetrieveCipherPrivilegeN(appId, encryptedPrivileges, checksum, pPrivilegeList);
141 if (r == E_DATA_NOT_FOUND)
143 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
146 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
148 r = privilegeInfo.Construct(appId, encryptedPrivileges, checksum, pPrivilegeList);
149 SysTryCatch(NID_SEC, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
151 r = privilegeInfo.GetBitwisePrivilegeN(pTargetBitwisePrivilege);
152 SysTryCatch(NID_SEC, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
154 pReturnPrivilegeList = UnpackPrivilegeN(pTargetBitwisePrivilege);
155 SysTryCatch(NID_SEC, pReturnPrivilegeList != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
161 if (pPrivilegeList != null)
163 pPrivilegeList->RemoveAll(true);
164 delete pPrivilegeList;
167 if (pTargetBitwisePrivilege)
169 free(pTargetBitwisePrivilege);
170 pTargetBitwisePrivilege = null;
173 return pReturnPrivilegeList;
177 _PrivilegeManager::RetrieveCipherPrivilegeN(const AppId& appId, Tizen::Base::String& encryptedPrivileges, Tizen::Base::String& checksum, ArrayList*& pPrivilegeList)
179 result r = E_SUCCESS;
180 ArrayList tempPrivilegeList;
182 encryptedPrivileges.Clear();
185 r = tempPrivilegeList.Construct();
186 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
188 _PackageInfoImpl infoImpl;
189 r = infoImpl.Construct(appId);
190 if (r == E_APP_NOT_INSTALLED)
192 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
193 return E_DATA_NOT_FOUND;
195 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
197 r = infoImpl.GetPrivileges(encryptedPrivileges, checksum, tempPrivilegeList);
198 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
199 SysTryReturnResult(NID_SEC, encryptedPrivileges.IsEmpty() != true, E_DATA_NOT_FOUND, "The privilege information does not exist.");
200 SysTryReturnResult(NID_SEC, checksum.IsEmpty() != true, E_SYSTEM, "An unexpected system error occurred.");
202 pPrivilegeList = new ArrayList;
203 r = pPrivilegeList->Construct();
205 IEnumerator* pEnum = tempPrivilegeList.GetEnumeratorN();
206 while (pEnum->MoveNext() == E_SUCCESS)
208 String* tempString = static_cast< String* >(pEnum->GetCurrent());
209 pPrivilegeList->Add(new String(*tempString));
212 tempPrivilegeList.RemoveAll(true);
217 _PrivilegeManager::RetrieveCipherPrivilegeExN(const AppId& appId, Tizen::Base::String& encryptedPrivileges, Tizen::Base::String& checksum, ArrayList*& pPrivilegeList)
219 result r = E_SUCCESS;
221 encryptedPrivileges.Clear();
224 String tempEncryptedPrivileges;
226 ArrayList tempPrivilegeList;
228 r = tempPrivilegeList.Construct();
229 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
231 _PackageInfoImpl infoImpl;
232 r = infoImpl.Construct(appId);
233 if (r == E_APP_NOT_INSTALLED)
235 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
236 return E_DATA_NOT_FOUND;
238 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
240 r = infoImpl.GetPrivileges(tempEncryptedPrivileges, tempChecksum, tempPrivilegeList);
241 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
242 SysTryReturnResult(NID_SEC, tempEncryptedPrivileges.IsEmpty() != true, E_DATA_NOT_FOUND, "The privilege information does not exist.");
243 SysTryReturnResult(NID_SEC, tempChecksum.IsEmpty() != true, E_SYSTEM, "An unexpected system error occurred.");
245 _PrivilegeInfo privilegeInfo;
247 r = privilegeInfo.Construct(appId, tempEncryptedPrivileges, tempChecksum, &tempPrivilegeList);
248 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
250 r = privilegeInfo.GetEncryptedBitwise(encryptedPrivileges);
251 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
253 r = privilegeInfo.GetChecksum(checksum);
254 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
256 pPrivilegeList = new ArrayList;
257 r = pPrivilegeList->Construct();
259 IEnumerator* pEnum = tempPrivilegeList.GetEnumeratorN();
260 while (pEnum->MoveNext() == E_SUCCESS)
262 String* tempString = static_cast< String* >(pEnum->GetCurrent());
263 pPrivilegeList->Add(new String(*tempString));
266 tempPrivilegeList.RemoveAll(true);
271 _PrivilegeManager::RetrievePrivilegeInfoN(const AppId& appId) const
273 result r = E_SUCCESS;
275 String encryptedPrivileges;
277 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
278 _PackageInfoImpl infoImpl;
279 ArrayList* pPrivilegeList = null;
283 pPrivilegeInfo.reset(__pPrivilegeCache->GetPrivilegeInfoN(appId));
290 else if (r == E_DATA_NOT_FOUND)
292 r = RetrieveCipherPrivilegeN(appId, encryptedPrivileges, checksum, pPrivilegeList);
293 if (r == E_DATA_NOT_FOUND)
295 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
298 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
300 pPrivilegeInfo.reset(new (std::nothrow) _PrivilegeInfo());
301 SysTryReturn(NID_SEC, pPrivilegeInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
303 r = pPrivilegeInfo->Construct(appId, encryptedPrivileges, checksum, pPrivilegeList);
304 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
306 r = __pPrivilegeCache->AddPrivilegeInfo(*(pPrivilegeInfo.get()));
307 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
309 if (pPrivilegeList != null)
311 pPrivilegeList->RemoveAll(true);
312 delete pPrivilegeList;
318 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
322 return pPrivilegeInfo.release();
327 _PrivilegeManager::UnpackPrivilegeN(const byte* pBitwisePrivilege)
329 result r = E_SUCCESS;
332 int bytePosition = 0;
334 int privilegeBit = 0;
335 int privilegeEnum = 0;
336 std::unique_ptr<ArrayList> pPrivilegeList(null);
340 SysTryReturn(NID_SEC, pBitwisePrivilege != null, null, E_INVALID_ARG, "One of the argument is invalid.");
342 pPrivilegeList.reset(new (std::nothrow) ArrayList());
343 SysTryReturn(NID_SEC, pPrivilegeList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
345 r = pPrivilegeList->Construct();
346 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
348 for (bytePosition = 0; bytePosition < MAX_BITWISE_PRIV_SIZE; bytePosition++)
352 if (!(pBitwisePrivilege[bytePosition] & 0xFF))
354 // Pass the checking of empty byte filed.
358 for (bitPosition = 0; bitPosition < _BITS_IN_BYTE; bitPosition++)
360 privilegeBit = (int) (pBitwisePrivilege[bytePosition] & maskFlag);
363 // Shift the maskFlag by 1 bit when face the empty bit.
364 maskFlag = maskFlag << 1;
368 privilegeEnum = (bytePosition * _BITS_IN_BYTE) + bitPosition;
370 for (index = 0; index < _MAX_PRIVILEGE_ENUM; index++)
372 if (privilegeEnum == privilegeListTable[index].privilege)
374 String* privilegeString = new (std::nothrow) String(L"http://tizen.org/privilege/");
375 privilegeString->Append(privilegeListTable[index].privilegeString);
376 SysTryCatch(NID_SEC, privilegeString != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
377 "[E_OUT_OF_MEMORY]The memory is insufficient.");
379 pPrivilegeList->Add(*privilegeString);
384 // Shift the maskFlag by 1 bit to check the next byte filed.
385 maskFlag = maskFlag << 1;
389 return pPrivilegeList.release();
393 pPrivilegeList->RemoveAll(true);
398 _PrivilegeManager::OnPackageInstallationCompleted(const PackageId& packageId, PackageInstallationResult installationResult)
400 __pPrivilegeCache->RemovePrivilegeInfo(packageId);
401 SysLog(NID_SEC, "%ls is removed.", packageId.GetPointer());