2 * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 * @author Sunggun Jung <sunggun.jung@samsung.com>
21 #include "TADC_Core.h"
24 #include "DrmTdcSvc.h"
25 #include "drm-tizen-mid.h"
26 #include "drm-tizen-apps.h"
27 #include "drm-tizen-error.h"
29 #include "DTapps2SqliteDB.h"
30 #include "DTapps2Base64.h"
31 #include "DTapps2HMAC.h"
32 #include "DTapps2Rights.h"
35 #define EXPORT_API __attribute__((visibility("default")))
38 /* Enable this flag to take API level time profiling */
39 //#define __DRM_TAPPS_API_TIME_PROFILING__
41 #ifdef __DRM_TAPPS_API_TIME_PROFILING__
46 DrmTappsProf(const char *function);
48 struct timeval TappsStartTv;
49 struct timeval TappsStopTv;
50 char TappsfunNane[256];
53 DrmTappsProf::DrmTappsProf(const char *function)
55 memcpy(TappsfunNane, function, strlen(function));
56 gettimeofday(&TappsStartTv, NULL);
57 DRM_TAPPS_EXCEPTION("[DRM-TIZEN-PERF]START:SEC=%ld, USEC=%ld for [%s]",
58 (long int)(TappsStartTv.tv_sec) , (long int)(TappsStartTv.tv_usec), function);
61 DrmTappsProf::~DrmTappsProf()
63 gettimeofday(&TappsStopTv, NULL);
64 DRM_TAPPS_EXCEPTION("[DRM-TIZEN-PERF] STOP:SEC=%ld, USEC=%ld for [%s]",
65 (long int)(TappsStopTv.tv_sec), (long int)(TappsStopTv.tv_usec), TappsfunNane);
66 DRM_TAPPS_EXCEPTION("[DRM-TIZEN-PERF]TOTAL_DIFFF : USEC=%ld for [%s]",
67 ((long int)(TappsStopTv.tv_sec - TappsStartTv.tv_sec) * (1000000) + (long int)(
68 TappsStopTv.tv_usec - TappsStartTv.tv_usec)), TappsfunNane);
71 #define DRM_TAPPS_API_TIME() DrmTappsProf DrmTappsObj(__func__);
73 #define DRM_TAPPS_API_TIME()
74 #endif /* __DRM_TAPPS_API_TIME_PROFILING__ */
76 int _drm_tapps_generate_purchase_request(
77 const char *pTADCFilepath,
78 char *pReqBuf, unsigned int *pReqBufLen, char *pLicenseUrl,
79 unsigned int *pLicenseUrlLen);
80 int _drm_tapps_generate_license_request(
81 const char *pRespBuf, unsigned int respBufLen,
82 char *pReqBuf, unsigned int *pReqBufLen,
83 char *pLicenseUrl, unsigned int *pLicenseUrlLen);
84 int _drm_tapps_register_license(const char *pRespBuf, unsigned int respBufLen);
85 int _drm_tapps_decrypt_package(const char *pTADCFilepath, int stadFileLen,
86 const char *pDecryptedFile, int decryptedFileLen);
87 int _drm_tapps_is_drm_file(const char *pDcfPath, int dcfPathLen);
90 int drm_tizen_generate_license_request(
91 const char *pRespBuf, unsigned int respBufLen,
92 char *pReqBuf, unsigned int *pReqBufLen,
93 char *pLicenseUrl, unsigned int *pLicenseUrlLen)
95 int ret = TADC_SUCCESS;
96 DRM_TAPPS_LOG("%s starts", __func__);
98 if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL) {
99 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
100 return TADC_PARAMETER_ERROR;
103 if (strlen(pRespBuf) != respBufLen) {
104 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.",
106 return TADC_PARAMETER_ERROR;
109 ret = _drm_tapps_generate_license_request(pRespBuf, respBufLen, pReqBuf,
110 pReqBufLen, pLicenseUrl, pLicenseUrlLen);
111 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
116 int _drm_tapps_generate_license_request(
117 const char *pRespBuf, unsigned int respBufLen,
118 char *pReqBuf, unsigned int *pReqBufLen,
119 char *pLicenseUrl, unsigned int *pLicenseUrlLen)
121 int ret = TADC_SUCCESS;
122 DRM_TAPPS_LOG("%s starts", __func__);
124 if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL) {
125 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
126 return TADC_PARAMETER_ERROR;
129 if (strlen(pRespBuf) != respBufLen) {
130 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.",
132 return TADC_PARAMETER_ERROR;
135 ret = DrmTdcGenerateLicenseRequest(pRespBuf, respBufLen, pReqBuf, pReqBufLen,
136 pLicenseUrl, pLicenseUrlLen);
137 DRM_TAPPS_LOG("%s result = %x", __func__, ret);
143 int drm_tizen_register_license(const char *pRespBuf, unsigned int respBufLen)
145 int ret = TADC_SUCCESS;
146 DRM_TAPPS_LOG("%s starts", __func__);
148 if (pRespBuf == NULL) {
149 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
150 return TADC_PARAMETER_ERROR;
153 if (strlen(pRespBuf) != respBufLen) {
154 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
155 return TADC_PARAMETER_ERROR;
158 ret = _drm_tapps_register_license(pRespBuf, respBufLen);
159 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
164 int _drm_tapps_register_license(const char *pRespBuf, unsigned int respBufLen)
168 int Ret = TADC_SUCCESS;
169 char pDecLicenseBuf[1024 * 8] = {0, };
170 unsigned int decLicenseBufLen = 0;
172 DRM_TAPPS_LOG("%s starts", __func__);
174 if (pRespBuf == NULL) {
175 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
176 return TADC_PARAMETER_ERROR;
179 if (strlen(pRespBuf) != respBufLen) {
180 DRM_TAPPS_EXCEPTION("%s) Input value and size weren't equal.", __func__);
181 return TADC_PARAMETER_ERROR;
184 memset(pDecLicenseBuf, 0x00, sizeof(pDecLicenseBuf));
185 decLicenseBufLen = sizeof(pDecLicenseBuf);
187 // Has to be enabled when the response from server is known
188 // which will be encrypted!!
189 Ret = DrmTdcDecryptLicense(pRespBuf, respBufLen, pDecLicenseBuf,
192 if (Ret != TADC_SUCCESS) {
193 DRM_TAPPS_EXCEPTION("DrmTdcDecryptLicense failed!!! Ret = %x", Ret);
197 Ret = DTappsInstallLicense(pDecLicenseBuf);
199 if (Ret != TADC_SUCCESS) {
200 DRM_TAPPS_EXCEPTION("DTappsInstallLicense failed!!! Ret = %x", Ret);
204 DRM_TAPPS_LOG("%s result = %x", __func__, Ret);
211 EXPORT_API int drm_tizen_is_drm_file(const char *pDcfPath, int dcfPathLen)
213 int ret = TADC_SUCCESS;
215 DRM_TAPPS_LOG("%s starts", __func__);
217 if (pDcfPath == NULL) {
218 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
219 return TADC_PARAMETER_ERROR;
222 if ((int)strlen(pDcfPath) != dcfPathLen) {
223 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
224 return TADC_PARAMETER_ERROR;
227 DRM_TAPPS_LOG("_drm_tizen_is_drm_file = %s started!", pDcfPath);
229 ret = _drm_tapps_is_drm_file(pDcfPath, dcfPathLen);
230 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
235 int _drm_tapps_is_drm_file(const char *pDcfPath, int dcfPathLen)
239 int ret = TADC_SUCCESS;
241 DrmTdcFileHeader fileHeader;
243 DRM_TAPPS_LOG("%s starts", __func__);
245 if (pDcfPath == NULL) {
246 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
247 ret = TADC_PARAMETER_ERROR;
251 if ((int)strlen(pDcfPath) != dcfPathLen) {
252 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
253 ret = TADC_PARAMETER_ERROR;
257 memset(&fileHeader, 0, sizeof(DrmTdcFileHeader));
258 bRet = DrmTdcGetFileHeader(pDcfPath, &fileHeader);
261 DRM_TAPPS_EXCEPTION("%s Error : DrmTdcGetFileHeader is failed", __func__);
262 ret = TADC_NOTTADCFILE_ERROR;
265 DRM_TAPPS_LOG("%s result = %x", __func__, ret);
272 int drm_tizen_decrypt_package(
273 const char *pTADCFilepath, int stadFileLen,
274 const char *pDecryptedFile, int decryptedFileLen)
276 int ret = TADC_SUCCESS;
277 DRM_TAPPS_LOG("drm_tizen_decrypt_package(%s, %s) started!", pTADCFilepath,
280 ret = _drm_tapps_decrypt_package(pTADCFilepath, stadFileLen, pDecryptedFile,
282 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
287 int _drm_tapps_decrypt_package(
288 const char *pTADCFilepath, int stadFileLen,
289 const char *pDecryptedFile, int decryptedFileLen)
294 int ret = TADC_SUCCESS;
297 DrmTdcFileHeader fileHeader;
299 memset(&t_RO, 0x00, sizeof(t_RO));
300 memset(&fileHeader, 0x00, sizeof(fileHeader));
302 DRM_TAPPS_LOG("%s starts", __func__);
304 if (pTADCFilepath == NULL || pDecryptedFile == NULL) {
305 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
306 return TADC_PARAMETER_ERROR;
309 if (((int)strlen(pTADCFilepath) != stadFileLen) ||
310 ((int)strlen(pDecryptedFile) != decryptedFileLen)) {
311 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
312 return TADC_PARAMETER_ERROR;
315 DRM_TAPPS_SECURE_LOG("%s) TADC File Path=%s, Decrypted File Path=%s", __func__,
316 pTADCFilepath, pDecryptedFile);
318 bRet = DrmTdcGetFileHeader(pTADCFilepath, &fileHeader);
321 DRM_TAPPS_EXCEPTION("DrmTdcGetFileHeader failed!!");
322 ret = TADC_GET_FILEHEADER_ERROR;
326 DRM_TAPPS_LOG("pTADCFilepath=%s, fileHeader.cid=%s, fileHeader.riurl=%s",
327 pTADCFilepath, fileHeader.cid, fileHeader.riurl);
329 bRet = DTappsGetCEK(fileHeader.cid, &t_RO);
332 DRM_TAPPS_EXCEPTION("DTappsGetCEK failed!! pTADCFilepath=%s, fileHeader.cid=%s",
333 pTADCFilepath, fileHeader.cid);
334 ret = TADC_GET_CEK_ERROR;
338 DRM_TAPPS_SECURE_LOG("fileHeader.cid=%s, t_RO.t_Content.CEK=%s", fileHeader.cid,
341 bRet = DrmTdcDecryptPackage2(pTADCFilepath, t_RO, pDecryptedFile);
345 "DrmTdcDecryptPackage2 failed!!, pTADCFilepath=%s, pDecryptedFile=%s",
346 pTADCFilepath, pDecryptedFile);
348 ret = TADC_DECRYPT_PACKAGE_ERROR;
354 if (t_RO.t_Content.CID) {
355 DTAPPS_FREE(t_RO.t_Content.CID);
358 if (t_RO.t_Content.CEK) {
359 DTAPPS_FREE(t_RO.t_Content.CEK);
362 if ((t_RO.PerFlag & DUID_RULE) && (t_RO.t_Permission.t_Individual.DUID)) {
363 DTAPPS_FREE(t_RO.t_Permission.t_Individual.DUID);
368 "%s failed!! pTADCFilepath=%s, pDecryptedFile=%s",
369 __func__, pTADCFilepath, pDecryptedFile);
372 DRM_TAPPS_LOG("%s Success!!", __func__);
378 int drm_tizen_generate_purchase_request(
379 const char *pTADCFilepath, char *pReqBuf, unsigned int *pReqBufLen,
380 char *pLicenseUrl, unsigned int *pLicenseUrlLen)
382 DRM_TAPPS_LOG("%s started!", __func__);
384 return _drm_tapps_generate_purchase_request(pTADCFilepath,
391 int _drm_tapps_generate_purchase_request(
392 const char *pTADCFilepath, char *pReqBuf, unsigned int *pReqBufLen,
393 char *pLicenseUrl, unsigned int *pLicenseUrlLen)
397 DRM_TAPPS_LOG("%s starts", __func__);
398 bool bRet = DrmTdcGeneratePurchaseRequest(
399 pTADCFilepath, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen);
401 return (bRet == FALSE) ? 0 : 1;