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"
34 /* Enable this flag to take API level time profiling */
35 //#define __DRM_TAPPS_API_TIME_PROFILING__
37 #ifdef __DRM_TAPPS_API_TIME_PROFILING__
43 DrmTappsProf(const char* function);
45 struct timeval TappsStartTv;
46 struct timeval TappsStopTv;
47 char TappsfunNane[256];
50 DrmTappsProf::DrmTappsProf(const char* function)
52 memcpy(TappsfunNane, function, strlen(function));
53 gettimeofday(&TappsStartTv, NULL);
54 DRM_TAPPS_EXCEPTION("[DRM-TIZEN-PERF]START:SEC=%ld, USEC=%ld for [%s]",(long int)(TappsStartTv.tv_sec) , (long int)(TappsStartTv.tv_usec),function);
57 DrmTappsProf::~DrmTappsProf()
59 gettimeofday(&TappsStopTv, NULL);
60 DRM_TAPPS_EXCEPTION("[DRM-TIZEN-PERF] STOP:SEC=%ld, USEC=%ld for [%s]", (long int)(TappsStopTv.tv_sec), (long int)(TappsStopTv.tv_usec),TappsfunNane);
61 DRM_TAPPS_EXCEPTION("[DRM-TIZEN-PERF]TOTAL_DIFFF : USEC=%ld for [%s]", ((long int)(TappsStopTv.tv_sec - TappsStartTv.tv_sec) * (1000000) + (long int)(TappsStopTv.tv_usec - TappsStartTv.tv_usec)), TappsfunNane);
64 #define DRM_TAPPS_API_TIME() DrmTappsProf DrmTappsObj(__func__);
66 #define DRM_TAPPS_API_TIME()
67 #endif /* __DRM_TAPPS_API_TIME_PROFILING__ */
73 /* Define EXPORT_API */
75 #define EXPORT_API __attribute__((visibility("default")))
78 int _drm_tapps_generate_purchase_request(const char *pTADCFilepath, char *pReqBuf, unsigned int *pReqBufLen, char *pLicenseUrl, unsigned int *pLicenseUrlLen);
79 int _drm_tapps_generate_license_request(const char *pRespBuf, unsigned int respBufLen, char *pReqBuf, unsigned int *pReqBufLen, char *pLicenseUrl, unsigned int *pLicenseUrlLen);
80 int _drm_tapps_register_license(const char *pRespBuf, unsigned int respBufLen);
81 int _drm_tapps_decrypt_package(const char *pTADCFilepath, int stadFileLen, const char *pDecryptedFile, int decryptedFileLen);
82 int _drm_tapps_is_drm_file(const char *pDcfPath, int dcfPathLen);
84 EXPORT_API int drm_tizen_generate_license_request(
85 const char *pRespBuf, //Response Data String of the Purchase Request ( Null terminator string )
86 unsigned int respBufLen, //pResBuf Length
87 char *pReqBuf, //License Request Data
88 unsigned int *pReqBufLen, //IN : pReqBuf Length, OUT : Rights Request Data String Size ( including null terminator )
89 char *pLicenseUrl, //License Acquisition URL Data
90 unsigned int *pLicenseUrlLen //IN : pLicenseUrl Length, OUT : Rights Issuer Server URL Data String Size ( including null terminator )
93 int ret = TADC_SUCCESS;
94 DRM_TAPPS_LOG("%s starts", __func__);
96 if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL)
98 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
99 return TADC_PARAMETER_ERROR;
102 if (strlen(pRespBuf) != respBufLen)
104 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.", __func__);
105 return TADC_PARAMETER_ERROR;
108 ret = _drm_tapps_generate_license_request(pRespBuf, respBufLen, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen);
109 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
114 int _drm_tapps_generate_license_request(
115 const char *pRespBuf, //Response Data String of the Purchase Request ( Null terminator string )
116 unsigned int respBufLen, //pResBuf Length
117 char *pReqBuf, //License Request Data
118 unsigned int *pReqBufLen, //IN : pReqBuf Length, OUT : Rights Request Data String Size ( including null terminator )
119 char *pLicenseUrl, //License Acquisition URL Data
120 unsigned int *pLicenseUrlLen //IN : pLicenseUrl Length, OUT : Rights Issuer Server URL Data String Size ( including null terminator )
123 int ret = TADC_SUCCESS;
124 DRM_TAPPS_LOG("%s starts", __func__);
126 if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL)
128 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
129 return TADC_PARAMETER_ERROR;
132 if (strlen(pRespBuf) != respBufLen)
134 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.", __func__);
135 return TADC_PARAMETER_ERROR;
138 ret = DrmTdcGenerateLicenseRequest( pRespBuf, respBufLen, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen );
139 DRM_TAPPS_LOG("%s result = %x", __func__, ret);
144 EXPORT_API int drm_tizen_register_license
146 const char *pRespBuf, //Response Data String of the Rights Request ( Null terminator string )
147 unsigned int respBufLen //pResBuf Length
150 int ret = TADC_SUCCESS;
151 DRM_TAPPS_LOG("%s starts", __func__);
153 if (pRespBuf == NULL)
155 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
156 return TADC_PARAMETER_ERROR;
159 if (strlen(pRespBuf) != respBufLen)
161 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
162 return TADC_PARAMETER_ERROR;
165 ret = _drm_tapps_register_license(pRespBuf, respBufLen);
166 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
171 int _drm_tapps_register_license(const char *pRespBuf, unsigned int respBufLen)
175 int Ret = TADC_SUCCESS;
176 char pDecLicenseBuf[1024*8] = {0, };
177 unsigned int decLicenseBufLen = 0;
179 DRM_TAPPS_LOG("%s starts", __func__);
183 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
184 return TADC_PARAMETER_ERROR;
187 if (strlen(pRespBuf) != respBufLen)
189 DRM_TAPPS_EXCEPTION("%s) Input value and size weren't equal.", __func__);
190 return TADC_PARAMETER_ERROR;
193 memset(pDecLicenseBuf, 0x00, sizeof(pDecLicenseBuf));
194 decLicenseBufLen = sizeof(pDecLicenseBuf);
196 /* Has to be enabled when the response from server is known which will be encrypted!! */
197 Ret = DrmTdcDecryptLicense(pRespBuf, respBufLen, pDecLicenseBuf, &decLicenseBufLen);
198 if (Ret != TADC_SUCCESS)
200 DRM_TAPPS_EXCEPTION("DrmTdcDecryptLicense failed!!! Ret = %x",Ret);
204 Ret = DTappsInstallLicense(pDecLicenseBuf);
205 if (Ret != TADC_SUCCESS)
207 DRM_TAPPS_EXCEPTION("DTappsInstallLicense failed!!! Ret = %x", Ret);
211 DRM_TAPPS_LOG("%s result = %x", __func__, Ret);
218 EXPORT_API int drm_tizen_is_drm_file(const char *pDcfPath, int dcfPathLen)
220 int ret = TADC_SUCCESS;
222 DRM_TAPPS_LOG("%s starts", __func__);
223 if (pDcfPath == NULL)
225 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
226 return TADC_PARAMETER_ERROR;
229 if ((int)strlen(pDcfPath) != dcfPathLen)
231 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
232 return TADC_PARAMETER_ERROR;
235 DRM_TAPPS_LOG("_drm_tizen_is_drm_file = %s started!", pDcfPath);
237 ret = _drm_tapps_is_drm_file(pDcfPath, dcfPathLen);
238 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
243 int _drm_tapps_is_drm_file(const char *pDcfPath, int dcfPathLen)
247 int ret = TADC_SUCCESS;
249 DrmTdcFileHeader fileHeader;
251 DRM_TAPPS_LOG("%s starts", __func__);
252 if (pDcfPath == NULL)
254 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
255 ret = TADC_PARAMETER_ERROR;
259 if ((int)strlen(pDcfPath) != dcfPathLen)
261 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
262 ret = TADC_PARAMETER_ERROR;
266 memset(&fileHeader, 0, sizeof(DrmTdcFileHeader));
267 bRet = DrmTdcGetFileHeader(pDcfPath, &fileHeader);
270 DRM_TAPPS_EXCEPTION("%s Error : DrmTdcGetFileHeader is failed", __func__);
271 ret = TADC_NOTTADCFILE_ERROR;
274 DRM_TAPPS_LOG("%s result = %x", __func__, ret);
280 EXPORT_API int drm_tizen_decrypt_package(
281 const char *pTADCFilepath, /* TDC DRM File Path */
283 const char *pDecryptedFile, /* Decrypted File Path */
287 int ret = TADC_SUCCESS;
288 DRM_TAPPS_LOG("drm_tizen_decrypt_package(%s, %s) started!", pTADCFilepath, pDecryptedFile);
290 ret = _drm_tapps_decrypt_package(pTADCFilepath, stadFileLen, pDecryptedFile, decryptedFileLen);
291 DRM_TAPPS_LOG("%s result=%x", __func__, ret);
296 int _drm_tapps_decrypt_package
298 const char *pTADCFilepath, //TDC DRM File Path
300 const char *pDecryptedFile, //Decrypted File Path
307 int ret = TADC_SUCCESS;
310 DrmTdcFileHeader fileHeader;
312 memset(&t_RO, 0x00, sizeof(t_RO));
313 memset(&fileHeader, 0x00, sizeof(fileHeader));
315 DRM_TAPPS_LOG("%s starts", __func__);
316 if (pTADCFilepath == NULL || pDecryptedFile == NULL)
318 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
319 return TADC_PARAMETER_ERROR;
322 if (((int)strlen(pTADCFilepath) != stadFileLen ) || ((int)strlen(pDecryptedFile) != decryptedFileLen))
324 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
325 return TADC_PARAMETER_ERROR;
327 DRM_TAPPS_SECURE_LOG("%s) TADC File Path=%s, Decrypted File Path=%s", __func__, pTADCFilepath, pDecryptedFile);
329 bRet = DrmTdcGetFileHeader(pTADCFilepath, &fileHeader);
332 DRM_TAPPS_EXCEPTION("DrmTdcGetFileHeader failed!!");
333 ret = TADC_GET_FILEHEADER_ERROR;
336 DRM_TAPPS_LOG("pTADCFilepath=%s, fileHeader.cid=%s, fileHeader.riurl=%s", pTADCFilepath, fileHeader.cid, fileHeader.riurl);
338 bRet = DTappsGetCEK(fileHeader.cid,&t_RO);
341 DRM_TAPPS_EXCEPTION("DTappsGetCEK failed!! pTADCFilepath=%s, fileHeader.cid=%s", pTADCFilepath, fileHeader.cid);
342 ret = TADC_GET_CEK_ERROR;
345 DRM_TAPPS_SECURE_LOG("fileHeader.cid=%s, t_RO.t_Content.CEK=%s", fileHeader.cid, t_RO.t_Content.CEK);
347 bRet = DrmTdcDecryptPackage2(pTADCFilepath, t_RO, pDecryptedFile);
350 DRM_TAPPS_EXCEPTION("DrmTdcDecryptPackage2 failed!!, pTADCFilepath=%s, pDecryptedFile=%s",pTADCFilepath,pDecryptedFile);
351 ret = TADC_DECRYPT_PACKAGE_ERROR;
356 if (t_RO.t_Content.CID)
358 DTAPPS_FREE(t_RO.t_Content.CID);
360 if (t_RO.t_Content.CEK)
362 DTAPPS_FREE(t_RO.t_Content.CEK);
364 if ((t_RO.PerFlag & DUID_RULE) && (t_RO.t_Permission.t_Individual.DUID))
366 DTAPPS_FREE(t_RO.t_Permission.t_Individual.DUID);
370 DRM_TAPPS_EXCEPTION("%s failed!! pTADCFilepath=%s, pDecryptedFile=%s", __func__, pTADCFilepath, pDecryptedFile);
375 DRM_TAPPS_LOG("%s Success!!", __func__);
380 EXPORT_API int drm_tizen_generate_purchase_request
382 const char *pTADCFilepath, //TDC DRM File Path
383 char *pReqBuf, //Purchase Request Data
384 unsigned int *pReqBufLen, //IN : pReqBuf Length, OUT : Purchase Request Data String Size ( including null terminator )
385 char *pLicenseUrl, //License Acquisition URL Data
386 unsigned int *pLicenseUrlLen //IN : pLicenseUrl Length, OUT : License Server URL Data String Size ( including null terminator )
389 DRM_TAPPS_LOG("%s started!", __func__);
391 return _drm_tapps_generate_purchase_request(pTADCFilepath,
398 int _drm_tapps_generate_purchase_request
400 const char *pTADCFilepath, //TDC DRM File Path
401 char *pReqBuf, //Purchase Request Data
402 unsigned int *pReqBufLen, //IN : pReqBuf Length, OUT : Purchase Request Data String Size ( including null terminator )
403 char *pLicenseUrl, //License Acquisition URL Data
404 unsigned int *pLicenseUrlLen //IN : pLicenseUrl Length, OUT : License Server URL Data String Size ( including null terminator )
411 DRM_TAPPS_LOG("%s starts", __func__);
412 bRet = DrmTdcGeneratePurchaseRequest(pTADCFilepath, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen);