2 // Open Service Platform
3 // Copyright (c) 2012-2013 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 DrmService.cpp
20 * @brief This is the implementation file for DrmService class.
23 #include "DrmService.h"
24 #include "DrmServiceDl.h"
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Io;
42 DrmService* DrmService::m_pInstance = null;
44 const char *DRM_SAPPS_API[] = {
45 "drm_oem_sapps_generate_license_request",
46 "drm_oem_sapps_register_license",
47 "drm_oem_sapps_is_drm_file",
48 "drm_oem_sapps_decrypt_package",
49 "drm_oem_sapps_generate_purchase_request",
53 DrmService::DrmService(void)
54 : m_sapps_handle(null)
56 , __pLocalMessagePort(null)
60 DrmService::~DrmService(void)
65 DrmService::GetInstance(void)
67 if (m_pInstance == null)
69 m_pInstance = new (std::nothrow) DrmService();
71 if (m_pInstance == null)
73 AppLogException("DrmService : Creating DrmService m_pInstance failed.");
78 m_pInstance->Construct();
79 AppLog("DrmService : m_pInstance is created.");
86 DrmService::FreeInstance(void)
88 if (m_pInstance != null)
96 DrmService::Construct(void)
98 static const wchar_t* LOCAL_MESSAGE_PORT_NAME = L"SAPPS_DRM_SERVER_PORT";
100 result r = E_SUCCESS;
102 AppLog("DrmService::Construct is called.");
104 __pLocalMessagePort = MessagePortManager::RequestLocalMessagePort(LOCAL_MESSAGE_PORT_NAME);
106 TryCatchTag(DRM_SERVICE, r == E_SUCCESS, r = E_SYSTEM, "[E_SYSTEM] DrmService failed to get LocalMessagePort instance, %s", GetErrorMessage(r));
107 AppLog("DrmService::RequestLocalMessagePort successfully called.");
109 __pLocalMessagePort->AddMessagePortListener(*this);
111 TryCatchTag(DRM_SERVICE, r == E_SUCCESS, r = E_SYSTEM, "[E_SYSTEM] DrmService failed to run AddMessagePortListner, %s", GetErrorMessage(r));
112 AppLog("DrmService : LocalMessagePort is ready.");
115 if (__pLocalMessagePort == null)
117 AppLogException("DrmService : RequestLocalMessagePort failed.");
125 DrmService::OnMessageReceivedN(RemoteMessagePort* pRemoteMessagePort, IMap* pMessage)
127 result r = E_SUCCESS;
128 String *pData = NULL;
130 HashMap *pMap = new HashMap(SingleObjectDeleter);
132 TryCatchTag(DRM_SERVICE, pMessage != NULL, r = E_INVALID_ARG, "DrmService : OnMessageReceivedN() failed. pMessage is invalid.");
133 TryCatchTag(DRM_SERVICE, pMap != NULL, r = E_OUT_OF_MEMORY, "DrmService : OnMessageReceivedN() failed. pMap is NULL.");
137 pData = static_cast<String *>(pMessage->GetValue(String(L"CLIENT_NAME")));
140 if (r == E_OBJ_NOT_FOUND)
142 AppLogException("Osp-Security-Service : [E_OBJ_NOT_FOUND] Failed to get CLIENT_NAME value.");
143 pMap->Add(new String(L"RESULT"), new String(L"CLIENT_NAME_NULL"));
147 if (m_isdlopened != true)
149 int retDrmDlCall = 0;
151 retDrmDlCall = DrmDlCall();
153 if (retDrmDlCall == DLOPEN_FAILED)
155 AppLogException("Osp-Security-Service : [DLOPEN_FAILED] Failed to load DRM-ENGINE");
156 pMap->Add(new String(L"RESULT"), new String(L"DLOPEN_FAILED"));
160 if (retDrmDlCall == DLSYM_FAILED)
162 AppLogException("Osp-Security-Service : [DLSYM_FAILED] Failed to load DRM-ENGINE.");
163 pMap->Add(new String(L"RESULT"), new String(L"DLSYM_FAILED"));
168 AppLog("Osp-Security-Service : Received length=%d, data=%ls", pData->GetLength(), pData->GetPointer());
170 if (pData->CompareTo(L"STORE_CLIENT") == 0)
173 char respBuf[1024*8] = {0,};
174 unsigned int respBufLen = sizeof(respBuf);
175 char reqBuf[1024] = {0,};
176 unsigned int reqBufLen = sizeof(reqBuf);
177 char licenseUrl[1024] = {0,};
178 unsigned int licenseUrlLen = sizeof(licenseUrl);
181 String *pDataAPI = static_cast<String *>(pMessage->GetValue(String(L"REQUEST_API")));
184 if (r == E_OBJ_NOT_FOUND)
186 AppLogException("DrmService : Failed to get REQUEST_API field.");
187 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_REQUEST_PROTOCOL_INVALID"));
191 if (pDataAPI->CompareTo(L"GENERATE_DRM_LICENSE_REQUEST") == 0)
193 String *pInputParam = static_cast<String *>(pMessage->GetValue(String(L"INPUT_PARAM")));
195 if (r == E_OBJ_NOT_FOUND)
197 AppLogException("DrmService : [GENERATE_DRM_LICENSE_REQUEST] Failed to get INPUT_PARAM.");
198 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_REQUEST_INPUT_PARAM_NULL"));
202 AppLog("API : %ls, INPUT_PARAM : %ls", pDataAPI->GetPointer(), pInputParam->GetPointer());
203 count = wcstombs(respBuf, pInputParam->GetPointer(), pInputParam->GetLength());
205 if (count != pInputParam->GetLength() || (int)strlen(respBuf) != count)
207 AppLogException("DrmService : [GENERATE_DRM_LICENSE_REQUEST] Failed to get the argument.");
208 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_REQUEST_INPUT_PARAM_INVALID"));
213 result = m_sapps_symbol.drm_oem_sapps_generate_license_request(respBuf, respBufLen, reqBuf, &reqBufLen, licenseUrl, &licenseUrlLen);
215 if (result != SADC_SUCCESS)
217 AppLogException("DrmService : [GENERATE_DRM_LICENSE_REQUEST] drm_oem_sapps_generate_license_request() failed. ret=%x", result);
218 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_REQUEST_INTERNAL_ERROR"));
222 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_REQUEST_SUCCESS"));
223 pMap->Add(new String(L"LICENSE_MSG"), new String(reqBuf));
224 pMap->Add(new String(L"LICENSE_URL"), new String(licenseUrl));
226 else if (pDataAPI->CompareTo(L"WRITE_DRM_LICESE") == 0)
228 String *pInputParam = static_cast<String *>(pMessage->GetValue(String(L"INPUT_PARAM")));
230 if (r == E_OBJ_NOT_FOUND)
232 AppLogException("DrmService : [WRITE_DRM_LICESE] Failed to get INPUT_PARAM.");
233 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_WRITE_INPUT_PARAM_NULL"));
237 AppLog("DrmService : API=%ls, INPUT_PARAM_Length=%d, INPUT_PARAM=%ls", pDataAPI->GetPointer(), pInputParam->GetLength() , pInputParam->GetPointer());
238 count = wcstombs(respBuf, pInputParam->GetPointer(), pInputParam->GetLength());
240 if (count != pInputParam->GetLength())
242 AppLogException("DrmService : [WRITE_DRM_LICESE] Failed to get the argument.");
243 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_WRITE_INPUT_PARAM_INVALID"));
248 result = m_sapps_symbol.drm_oem_sapps_register_license(respBuf, respBufLen);
250 if (result != SADC_SUCCESS)
252 AppLogException("DrmService : [WRITE_DRM_LICESE] drm_oem_sapps_register_license() failed. ret=%x", result);
253 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_WRITE_INTERNAL_ERROR"));
257 pMap->Add(new String(L"RESULT"), new String(L"LICENSE_WRITE_SUCCESS"));
261 AppLogException("DrmService : UNSURPPORTED_API requested.");
262 pMap->Add(new String(L"RESULT"), new String(L"UNSURPPORTED_API"));
268 AppLogException("DrmService : Unauthorized client called.");
269 pMap->Add(new String(L"RESULT"), new String(L"UNAUTHORIZED_CLIENT"));
274 pRemoteMessagePort->SendMessage(__pLocalMessagePort, pMap);
276 AppLogExceptionIf(r != E_SUCCESS, "DrmService : [E_SYSTEM] DrmService failed to SendMessage(), %s", GetErrorMessage(r));
282 if (pMessage != NULL)
289 DrmService::DrmDlCall(void)
292 int ret = SADC_SUCCESS;
294 void *sappshandle[FUNC_DRM_SAPPS_MAX] = {0,};
296 AppLog("DrmService : DrmDlCall is called.");
298 m_sapps_handle = dlopen(DRM_SAPPS_SO_PATH, RTLD_LAZY);
300 TryCatchTag(DRM_SERVICE, error == NULL, ret = DLOPEN_FAILED, "DrmService : dlopen() failed. err=%s", error);
301 AppLog("Osp-Security-Service : dlopen success.");
303 for (int i = 0; i < FUNC_DRM_SAPPS_MAX ; i++)
305 handle = dlsym(m_sapps_handle, DRM_SAPPS_API[i]);
308 TryCatchTag(DRM_SERVICE, error == NULL, ret = DLSYM_FAILED, "DrmService : dlsym() failed. err=%s", error);
309 AppLog("DrmService : drm_sapps_api is loaded : dlsym(%d)\n", i);
311 sappshandle[i] = handle;
314 memcpy(&m_sapps_symbol, sappshandle, sizeof(sappshandle));
317 AppLog("DrmService : DrmDlCall is totally success.");