93eec5e971796d4c46d0d57b20f6b53ed2fe7143
[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 #ifndef EXPORT_API
35 #define EXPORT_API __attribute__((visibility("default")))
36 #endif
37
38 /* Enable this flag to take API level time profiling */
39 //#define __DRM_TAPPS_API_TIME_PROFILING__
40
41 #ifdef __DRM_TAPPS_API_TIME_PROFILING__
42 #include <sys/time.h>
43
44 class DrmTappsProf {
45 public:
46         DrmTappsProf(const char *function);
47         ~DrmTappsProf();
48         struct timeval TappsStartTv;
49         struct timeval TappsStopTv;
50         char TappsfunNane[256];
51 };
52
53 DrmTappsProf::DrmTappsProf(const char *function)
54 {
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);
59 }
60
61 DrmTappsProf::~DrmTappsProf()
62 {
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);
69 }
70
71 #define DRM_TAPPS_API_TIME() DrmTappsProf DrmTappsObj(__func__);
72 #else
73 #define DRM_TAPPS_API_TIME()
74 #endif /* __DRM_TAPPS_API_TIME_PROFILING__ */
75
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);
88
89 EXPORT_API
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)
94 {
95         int ret = TADC_SUCCESS;
96         DRM_TAPPS_LOG("%s starts", __func__);
97
98         if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL) {
99                 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
100                 return TADC_PARAMETER_ERROR;
101         }
102
103         if (strlen(pRespBuf) != respBufLen) {
104                 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.",
105                                                         __func__);
106                 return TADC_PARAMETER_ERROR;
107         }
108
109         ret = _drm_tapps_generate_license_request(pRespBuf, respBufLen, pReqBuf,
110                         pReqBufLen, pLicenseUrl, pLicenseUrlLen);
111         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
112
113         return ret;
114 }
115
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)
120 {
121         int ret = TADC_SUCCESS;
122         DRM_TAPPS_LOG("%s starts", __func__);
123
124         if (pRespBuf == NULL || pReqBuf == NULL || pLicenseUrl == NULL) {
125                 DRM_TAPPS_EXCEPTION("%s) Error : Parameters NULL.", __func__);
126                 return TADC_PARAMETER_ERROR;
127         }
128
129         if (strlen(pRespBuf) != respBufLen) {
130                 DRM_TAPPS_EXCEPTION("%s) Error : Input value and size weren't equal.",
131                                                         __func__);
132                 return TADC_PARAMETER_ERROR;
133         }
134
135         ret = DrmTdcGenerateLicenseRequest(pRespBuf, respBufLen, pReqBuf, pReqBufLen,
136                                                                            pLicenseUrl, pLicenseUrlLen);
137         DRM_TAPPS_LOG("%s result = %x", __func__, ret);
138
139         return ret;
140 }
141
142 EXPORT_API
143 int drm_tizen_register_license(const char *pRespBuf, unsigned int respBufLen)
144 {
145         int ret = TADC_SUCCESS;
146         DRM_TAPPS_LOG("%s starts", __func__);
147
148         if (pRespBuf == NULL) {
149                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
150                 return TADC_PARAMETER_ERROR;
151         }
152
153         if (strlen(pRespBuf) != respBufLen) {
154                 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
155                 return TADC_PARAMETER_ERROR;
156         }
157
158         ret = _drm_tapps_register_license(pRespBuf, respBufLen);
159         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
160
161         return ret;
162 }
163
164 int _drm_tapps_register_license(const char *pRespBuf, unsigned int respBufLen)
165 {
166         DRM_TAPPS_API_TIME()
167
168         int Ret = TADC_SUCCESS;
169         char pDecLicenseBuf[1024 * 8] = {0, };
170         unsigned int decLicenseBufLen = 0;
171
172         DRM_TAPPS_LOG("%s starts", __func__);
173
174         if (pRespBuf == NULL) {
175                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
176                 return TADC_PARAMETER_ERROR;
177         }
178
179         if (strlen(pRespBuf) != respBufLen) {
180                 DRM_TAPPS_EXCEPTION("%s) Input value and size weren't equal.", __func__);
181                 return TADC_PARAMETER_ERROR;
182         }
183
184         memset(pDecLicenseBuf, 0x00, sizeof(pDecLicenseBuf));
185         decLicenseBufLen = sizeof(pDecLicenseBuf);
186
187         // Has to be enabled when the response from server is known
188         // which will be encrypted!!
189         Ret = DrmTdcDecryptLicense(pRespBuf, respBufLen, pDecLicenseBuf,
190                                                            &decLicenseBufLen);
191
192         if (Ret != TADC_SUCCESS) {
193                 DRM_TAPPS_EXCEPTION("DrmTdcDecryptLicense failed!!! Ret = %x", Ret);
194                 return Ret;
195         }
196
197         Ret = DTappsInstallLicense(pDecLicenseBuf);
198
199         if (Ret != TADC_SUCCESS) {
200                 DRM_TAPPS_EXCEPTION("DTappsInstallLicense failed!!! Ret = %x", Ret);
201                 goto DTAPPS_EXIT;
202         }
203
204         DRM_TAPPS_LOG("%s result = %x", __func__, Ret);
205
206 DTAPPS_EXIT:
207         return Ret;
208 }
209
210
211 EXPORT_API int drm_tizen_is_drm_file(const char *pDcfPath, int dcfPathLen)
212 {
213         int ret = TADC_SUCCESS;
214
215         DRM_TAPPS_LOG("%s starts", __func__);
216
217         if (pDcfPath == NULL) {
218                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
219                 return TADC_PARAMETER_ERROR;
220         }
221
222         if ((int)strlen(pDcfPath) != dcfPathLen) {
223                 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
224                 return TADC_PARAMETER_ERROR;
225         }
226
227         DRM_TAPPS_LOG("_drm_tizen_is_drm_file = %s started!", pDcfPath);
228
229         ret = _drm_tapps_is_drm_file(pDcfPath, dcfPathLen);
230         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
231
232         return ret;
233 }
234
235 int _drm_tapps_is_drm_file(const char *pDcfPath, int dcfPathLen)
236 {
237         DRM_TAPPS_API_TIME()
238
239         int ret = TADC_SUCCESS;
240         bool bRet = FALSE;
241         DrmTdcFileHeader fileHeader;
242
243         DRM_TAPPS_LOG("%s starts", __func__);
244
245         if (pDcfPath == NULL) {
246                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
247                 ret = TADC_PARAMETER_ERROR;
248                 goto finish;
249         }
250
251         if ((int)strlen(pDcfPath) != dcfPathLen) {
252                 DRM_TAPPS_EXCEPTION("%s) Input value and size wasn't equal.", __func__);
253                 ret = TADC_PARAMETER_ERROR;
254                 goto finish;
255         }
256
257         memset(&fileHeader, 0, sizeof(DrmTdcFileHeader));
258         bRet = DrmTdcGetFileHeader(pDcfPath, &fileHeader);
259
260         if (bRet == FALSE) {
261                 DRM_TAPPS_EXCEPTION("%s Error : DrmTdcGetFileHeader is failed", __func__);
262                 ret = TADC_NOTTADCFILE_ERROR;
263         }
264
265         DRM_TAPPS_LOG("%s result = %x", __func__, ret);
266
267 finish:
268         return ret;
269 }
270
271 EXPORT_API
272 int drm_tizen_decrypt_package(
273         const char *pTADCFilepath, int stadFileLen,
274         const char *pDecryptedFile, int decryptedFileLen)
275 {
276         int ret = TADC_SUCCESS;
277         DRM_TAPPS_LOG("drm_tizen_decrypt_package(%s, %s) started!", pTADCFilepath,
278                                   pDecryptedFile);
279
280         ret = _drm_tapps_decrypt_package(pTADCFilepath, stadFileLen, pDecryptedFile,
281                                                                          decryptedFileLen);
282         DRM_TAPPS_LOG("%s result=%x", __func__, ret);
283
284         return ret;
285 }
286
287 int _drm_tapps_decrypt_package(
288         const char *pTADCFilepath, int stadFileLen,
289         const char *pDecryptedFile, int decryptedFileLen)
290 {
291         DRM_TAPPS_API_TIME()
292
293         bool bRet = true;
294         int ret = TADC_SUCCESS;
295
296         T_RO t_RO;
297         DrmTdcFileHeader fileHeader;
298
299         memset(&t_RO, 0x00, sizeof(t_RO));
300         memset(&fileHeader, 0x00, sizeof(fileHeader));
301
302         DRM_TAPPS_LOG("%s starts", __func__);
303
304         if (pTADCFilepath == NULL || pDecryptedFile == NULL) {
305                 DRM_TAPPS_EXCEPTION("%s) Parameters NULL!", __func__);
306                 return TADC_PARAMETER_ERROR;
307         }
308
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;
313         }
314
315         DRM_TAPPS_SECURE_LOG("%s) TADC File Path=%s, Decrypted File Path=%s", __func__,
316                                                  pTADCFilepath, pDecryptedFile);
317
318         bRet = DrmTdcGetFileHeader(pTADCFilepath, &fileHeader);
319
320         if (FALSE == bRet) {
321                 DRM_TAPPS_EXCEPTION("DrmTdcGetFileHeader failed!!");
322                 ret = TADC_GET_FILEHEADER_ERROR;
323                 goto TAPPS_END;
324         }
325
326         DRM_TAPPS_LOG("pTADCFilepath=%s, fileHeader.cid=%s, fileHeader.riurl=%s",
327                                   pTADCFilepath, fileHeader.cid, fileHeader.riurl);
328
329         bRet = DTappsGetCEK(fileHeader.cid, &t_RO);
330
331         if (FALSE == bRet) {
332                 DRM_TAPPS_EXCEPTION("DTappsGetCEK failed!! pTADCFilepath=%s, fileHeader.cid=%s",
333                                                         pTADCFilepath, fileHeader.cid);
334                 ret = TADC_GET_CEK_ERROR;
335                 goto TAPPS_END;
336         }
337
338         DRM_TAPPS_SECURE_LOG("fileHeader.cid=%s, t_RO.t_Content.CEK=%s", fileHeader.cid,
339                                                  t_RO.t_Content.CEK);
340
341         bRet = DrmTdcDecryptPackage2(pTADCFilepath, t_RO, pDecryptedFile);
342
343         if (FALSE == bRet) {
344                 DRM_TAPPS_EXCEPTION(
345                         "DrmTdcDecryptPackage2 failed!!, pTADCFilepath=%s, pDecryptedFile=%s",
346                         pTADCFilepath, pDecryptedFile);
347
348                 ret = TADC_DECRYPT_PACKAGE_ERROR;
349                 goto TAPPS_END;
350         }
351
352 TAPPS_END:
353
354         if (t_RO.t_Content.CID) {
355                 DTAPPS_FREE(t_RO.t_Content.CID);
356         }
357
358         if (t_RO.t_Content.CEK) {
359                 DTAPPS_FREE(t_RO.t_Content.CEK);
360         }
361
362         if ((t_RO.PerFlag & DUID_RULE) && (t_RO.t_Permission.t_Individual.DUID)) {
363                 DTAPPS_FREE(t_RO.t_Permission.t_Individual.DUID);
364         }
365
366         if (bRet == FALSE) {
367                 DRM_TAPPS_EXCEPTION(
368                         "%s failed!! pTADCFilepath=%s, pDecryptedFile=%s",
369                         __func__, pTADCFilepath, pDecryptedFile);
370                 return ret;
371         } else {
372                 DRM_TAPPS_LOG("%s Success!!", __func__);
373                 return ret;
374         }
375 }
376
377 EXPORT_API
378 int drm_tizen_generate_purchase_request(
379         const char *pTADCFilepath, char *pReqBuf, unsigned int *pReqBufLen,
380         char *pLicenseUrl, unsigned int *pLicenseUrlLen)
381 {
382         DRM_TAPPS_LOG("%s started!", __func__);
383
384         return _drm_tapps_generate_purchase_request(pTADCFilepath,
385                         pReqBuf,
386                         pReqBufLen,
387                         pLicenseUrl,
388                         pLicenseUrlLen);
389 }
390
391 int _drm_tapps_generate_purchase_request(
392         const char *pTADCFilepath, char *pReqBuf, unsigned int *pReqBufLen,
393         char *pLicenseUrl, unsigned int *pLicenseUrlLen)
394 {
395         DRM_TAPPS_API_TIME()
396
397         DRM_TAPPS_LOG("%s starts", __func__);
398         bool bRet = DrmTdcGeneratePurchaseRequest(
399                                         pTADCFilepath, pReqBuf, pReqBufLen, pLicenseUrl, pLicenseUrlLen);
400
401         return (bRet == FALSE) ? 0 : 1;
402 }