Fix build warning and add related gcc options
[platform/core/security/drm-service-core-tizen.git] / service / drm-tapps.cpp
1 /*
2  * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  *
16  * @file     drm-tapps.cpp
17  * @author   Sunggun Jung    <sunggun.jung@samsung.com>
18  *
19  */
20
21 #include "TADC_Core.h"
22 #include "TADC_IF.h"
23
24 #include "DrmTdcSvc.h"
25 #include "drm-tizen-mid.h"
26 #include "drm-tizen-apps.h"
27 #include "drm-tizen-error.h"
28
29 #include "DTapps2SqliteDB.h"
30 #include "DTapps2Base64.h"
31 #include "DTapps2HMAC.h"
32 #include "DTapps2Rights.h"
33
34 /* Enable this flag to take API level time profiling */
35 //#define __DRM_TAPPS_API_TIME_PROFILING__
36
37 #ifdef __DRM_TAPPS_API_TIME_PROFILING__
38 #include <sys/time.h>
39
40 class DrmTappsProf
41 {
42 public:
43         DrmTappsProf(const char* function);
44         ~DrmTappsProf();
45         struct timeval TappsStartTv;
46         struct timeval TappsStopTv;
47         char TappsfunNane[256];
48 };
49
50 DrmTappsProf::DrmTappsProf(const char* function)
51 {
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);
55 }
56
57 DrmTappsProf::~DrmTappsProf()
58 {
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);
62 }
63
64 #define DRM_TAPPS_API_TIME() DrmTappsProf DrmTappsObj(__func__);
65 #else
66 #define DRM_TAPPS_API_TIME()
67 #endif /* __DRM_TAPPS_API_TIME_PROFILING__ */
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72
73 /* Define EXPORT_API */
74 #ifndef EXPORT_API
75 #define EXPORT_API __attribute__((visibility("default")))
76 #endif
77
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);
83
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 )
91 )
92 {
93         int ret = TADC_SUCCESS;
94         DRM_TAPPS_LOG("%s starts", __func__);
95
96         if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL)
97         {
98                 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
99                 return TADC_PARAMETER_ERROR;
100         }
101
102         if (strlen(pRespBuf) != respBufLen)
103         {
104                 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.", __func__);
105                 return TADC_PARAMETER_ERROR;
106         }
107
108         ret = _drm_tapps_generate_license_request(pRespBuf, respBufLen, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen);
109         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
110
111         return ret;
112 }
113
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 )
121 )
122 {
123         int ret = TADC_SUCCESS;
124         DRM_TAPPS_LOG("%s starts", __func__);
125
126         if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL)
127         {
128                 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
129                 return TADC_PARAMETER_ERROR;
130         }
131
132         if (strlen(pRespBuf) != respBufLen)
133         {
134                 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.", __func__);
135                 return TADC_PARAMETER_ERROR;
136         }
137
138         ret = DrmTdcGenerateLicenseRequest( pRespBuf, respBufLen, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen );
139         DRM_TAPPS_LOG("%s result = %x", __func__, ret);
140
141         return ret;
142 }
143
144 EXPORT_API int drm_tizen_register_license
145 (
146         const char *pRespBuf,                           //Response Data String of the Rights Request ( Null terminator string )
147         unsigned int respBufLen                         //pResBuf Length
148 )
149 {
150         int ret = TADC_SUCCESS;
151         DRM_TAPPS_LOG("%s starts", __func__);
152
153         if (pRespBuf == NULL)
154         {
155                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
156                 return TADC_PARAMETER_ERROR;
157         }
158
159         if (strlen(pRespBuf) != respBufLen)
160         {
161                 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
162                 return TADC_PARAMETER_ERROR;
163         }
164
165         ret = _drm_tapps_register_license(pRespBuf, respBufLen);
166         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
167
168         return ret;
169 }
170
171 int _drm_tapps_register_license(const char *pRespBuf, unsigned int respBufLen)
172 {
173         DRM_TAPPS_API_TIME()
174
175         int Ret = TADC_SUCCESS;
176         char pDecLicenseBuf[1024*8] = {0, };
177         unsigned int decLicenseBufLen = 0;
178
179         DRM_TAPPS_LOG("%s starts", __func__);
180
181         if(pRespBuf == NULL)
182         {
183                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
184                 return TADC_PARAMETER_ERROR;
185         }
186
187         if (strlen(pRespBuf) != respBufLen)
188         {
189                 DRM_TAPPS_EXCEPTION("%s) Input value and size weren't equal.", __func__);
190                 return TADC_PARAMETER_ERROR;
191         }
192
193         memset(pDecLicenseBuf, 0x00, sizeof(pDecLicenseBuf));
194         decLicenseBufLen = sizeof(pDecLicenseBuf);
195
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)
199         {
200                 DRM_TAPPS_EXCEPTION("DrmTdcDecryptLicense failed!!! Ret = %x",Ret);
201                 return Ret;
202         }
203
204         Ret = DTappsInstallLicense(pDecLicenseBuf);
205         if (Ret != TADC_SUCCESS)
206         {
207                 DRM_TAPPS_EXCEPTION("DTappsInstallLicense failed!!! Ret = %x", Ret);
208                 goto DTAPPS_EXIT;
209         }
210
211         DRM_TAPPS_LOG("%s result = %x", __func__, Ret);
212
213 DTAPPS_EXIT:
214         return Ret;
215 }
216
217
218 EXPORT_API int drm_tizen_is_drm_file(const char *pDcfPath, int dcfPathLen)
219 {
220         int ret = TADC_SUCCESS;
221
222         DRM_TAPPS_LOG("%s starts", __func__);
223         if (pDcfPath == NULL)
224         {
225                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
226                 return TADC_PARAMETER_ERROR;
227         }
228
229         if ((int)strlen(pDcfPath) != dcfPathLen)
230         {
231                 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
232                 return TADC_PARAMETER_ERROR;
233         }
234
235         DRM_TAPPS_LOG("_drm_tizen_is_drm_file = %s started!", pDcfPath);
236
237         ret = _drm_tapps_is_drm_file(pDcfPath, dcfPathLen);
238         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
239
240         return ret;
241 }
242
243 int _drm_tapps_is_drm_file(const char *pDcfPath, int dcfPathLen)
244 {
245         DRM_TAPPS_API_TIME()
246
247         int ret = TADC_SUCCESS;
248         bool bRet = FALSE;
249         DrmTdcFileHeader fileHeader;
250
251         DRM_TAPPS_LOG("%s starts", __func__);
252         if (pDcfPath == NULL)
253         {
254                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
255                 ret = TADC_PARAMETER_ERROR;
256                 goto finish;
257         }
258
259         if ((int)strlen(pDcfPath) != dcfPathLen)
260         {
261                 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
262                 ret = TADC_PARAMETER_ERROR;
263                 goto finish;
264         }
265
266         memset(&fileHeader, 0, sizeof(DrmTdcFileHeader));
267         bRet = DrmTdcGetFileHeader(pDcfPath, &fileHeader);
268         if (bRet == FALSE)
269         {
270                 DRM_TAPPS_EXCEPTION("%s Error : DrmTdcGetFileHeader is failed", __func__);
271                 ret = TADC_NOTTADCFILE_ERROR;
272         }
273
274         DRM_TAPPS_LOG("%s result = %x", __func__, ret);
275
276 finish:
277         return ret;
278 }
279
280 EXPORT_API int drm_tizen_decrypt_package(
281  const char *pTADCFilepath,   /* TDC DRM File Path  */
282  int stadFileLen,
283  const char *pDecryptedFile,   /* Decrypted File Path */
284  int decryptedFileLen
285 )
286 {
287         int ret = TADC_SUCCESS;
288         DRM_TAPPS_LOG("drm_tizen_decrypt_package(%s, %s) started!", pTADCFilepath, pDecryptedFile);
289
290         ret = _drm_tapps_decrypt_package(pTADCFilepath, stadFileLen, pDecryptedFile, decryptedFileLen);
291         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
292
293         return ret;
294 }
295
296 int _drm_tapps_decrypt_package
297 (
298  const char *pTADCFilepath,   //TDC DRM File Path
299  int stadFileLen,
300  const char *pDecryptedFile,   //Decrypted File Path
301  int decryptedFileLen
302 )
303 {
304         DRM_TAPPS_API_TIME()
305
306         bool bRet = true;
307         int ret = TADC_SUCCESS;
308
309         T_RO t_RO;
310         DrmTdcFileHeader fileHeader;
311
312         memset(&t_RO, 0x00, sizeof(t_RO));
313         memset(&fileHeader, 0x00, sizeof(fileHeader));
314
315         DRM_TAPPS_LOG("%s starts", __func__);
316         if (pTADCFilepath == NULL || pDecryptedFile == NULL)
317         {
318                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
319                 return TADC_PARAMETER_ERROR;
320         }
321
322         if (((int)strlen(pTADCFilepath) != stadFileLen ) || ((int)strlen(pDecryptedFile) != decryptedFileLen))
323         {
324                 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
325                 return TADC_PARAMETER_ERROR;
326         }
327         DRM_TAPPS_SECURE_LOG("%s) TADC File Path=%s, Decrypted File Path=%s", __func__, pTADCFilepath, pDecryptedFile);
328
329         bRet = DrmTdcGetFileHeader(pTADCFilepath, &fileHeader);
330         if (FALSE == bRet)
331         {
332                 DRM_TAPPS_EXCEPTION("DrmTdcGetFileHeader failed!!");
333                 ret = TADC_GET_FILEHEADER_ERROR;
334                 goto TAPPS_END;
335         }
336         DRM_TAPPS_LOG("pTADCFilepath=%s, fileHeader.cid=%s, fileHeader.riurl=%s", pTADCFilepath, fileHeader.cid, fileHeader.riurl);
337
338         bRet = DTappsGetCEK(fileHeader.cid,&t_RO);
339         if(FALSE == bRet)
340         {
341                 DRM_TAPPS_EXCEPTION("DTappsGetCEK failed!! pTADCFilepath=%s, fileHeader.cid=%s", pTADCFilepath, fileHeader.cid);
342                 ret = TADC_GET_CEK_ERROR;
343                 goto TAPPS_END;
344         }
345         DRM_TAPPS_SECURE_LOG("fileHeader.cid=%s, t_RO.t_Content.CEK=%s", fileHeader.cid, t_RO.t_Content.CEK);
346
347         bRet = DrmTdcDecryptPackage2(pTADCFilepath, t_RO, pDecryptedFile);
348         if(FALSE == bRet)
349         {
350                 DRM_TAPPS_EXCEPTION("DrmTdcDecryptPackage2 failed!!, pTADCFilepath=%s, pDecryptedFile=%s",pTADCFilepath,pDecryptedFile);
351                 ret = TADC_DECRYPT_PACKAGE_ERROR;
352                 goto TAPPS_END;
353         }
354
355 TAPPS_END:
356         if (t_RO.t_Content.CID)
357         {
358                 DTAPPS_FREE(t_RO.t_Content.CID);
359         }
360         if (t_RO.t_Content.CEK)
361         {
362                 DTAPPS_FREE(t_RO.t_Content.CEK);
363         }
364         if ((t_RO.PerFlag & DUID_RULE) && (t_RO.t_Permission.t_Individual.DUID))
365         {
366                 DTAPPS_FREE(t_RO.t_Permission.t_Individual.DUID);
367         }
368         if (bRet == FALSE)
369         {
370                 DRM_TAPPS_EXCEPTION("%s failed!! pTADCFilepath=%s, pDecryptedFile=%s", __func__, pTADCFilepath, pDecryptedFile);
371                 return ret;
372         }
373         else
374         {
375                 DRM_TAPPS_LOG("%s Success!!", __func__);
376                 return ret;
377         }
378 }
379
380 EXPORT_API int drm_tizen_generate_purchase_request
381 (
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 )
387 )
388 {
389         DRM_TAPPS_LOG("%s started!", __func__);
390
391         return _drm_tapps_generate_purchase_request(pTADCFilepath,
392                                                                                                 pReqBuf,
393                                                                                                 pReqBufLen,
394                                                                                                 pLicenseUrl,
395                                                                                                 pLicenseUrlLen);
396 }
397
398 int _drm_tapps_generate_purchase_request
399 (
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 )
405 )
406 {
407         DRM_TAPPS_API_TIME()
408
409         bool bRet = FALSE;
410
411         DRM_TAPPS_LOG("%s starts", __func__);
412         bRet = DrmTdcGeneratePurchaseRequest(pTADCFilepath, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen);
413         if (bRet == FALSE)
414         {
415                 return 0;
416         }
417     else
418         {
419                 return 1;
420         }
421 }
422
423 #ifdef __cplusplus
424 }
425 #endif