Fix uncheck return of vconf_get_bool
[platform/core/messaging/msg-service.git] / utils / MsgDrmWrapper.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
17 #include "MsgDebug.h"
18 #include "MsgDrmWrapper.h"
19 #include "MsgUtilFile.h"
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <dirent.h>
25 #include <string.h>
26 #include <sys/vfs.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33
34 #if MSG_DRM_SUPPORT
35
36 #include <drm_client_types.h>
37 #include <drm_client.h>
38
39 #endif /* MSG_DRM_SUPPORT */
40
41 #define MSG_MAX_DRM_FILE_PATH MSG_FILEPATH_LEN_MAX
42
43 bool MsgDrmRegisterFile(MSG_DRM_OPENMODE eMode, char *pBuffer, int nSize)
44 {
45 #if MSG_DRM_SUPPORT
46         if (eMode == MSG_MODE_STREAM) {
47                 MSG_DEBUG("Fail(eMode == MSG_MODE_STREAM)");
48                 return false;
49         }
50
51         if (pBuffer == NULL) {
52                 MSG_DEBUG("[Error] pBuffer is NULL");
53                 return false;
54         }
55
56         MSG_DEBUG("buffer = %s, nSize = %d", pBuffer, nSize);
57
58         drm_bool_type_e isDrm;
59         int eDRMResult = drm_is_drm_file(pBuffer, &isDrm);
60
61         if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
62                 MSG_DEBUG("file is not drm file");
63                 return false;
64         }
65
66         drm_request_type_e request_type = DRM_REQUEST_TYPE_REGISTER_FILE;
67
68         eDRMResult = drm_process_request(request_type, pBuffer, NULL);
69         if (DRM_RETURN_SUCCESS != eDRMResult) {
70                 MSG_DEBUG("drm_process_request is failed : 0x%x", eDRMResult);
71                 return false;
72         }
73         MSG_END();
74         return true;
75 #else
76         return false;
77 #endif
78 }
79
80 bool MsgDrmUnregisterFile(char *szFilename)
81 {
82 #if MSG_DRM_SUPPORT
83         if (szFilename == NULL) {
84                 MSG_DEBUG("[Error] szFilename is NULL");
85                 return false;
86         }
87
88         MSG_DEBUG("szFilename = %s", szFilename);
89
90         drm_request_type_e request_type = DRM_REQUEST_TYPE_UNREGISTER_FILE;
91
92         int eDRMResult = drm_process_request(request_type, szFilename, NULL); /* Unregister a DCF file */
93         if (DRM_RETURN_SUCCESS != eDRMResult) {
94                 MSG_DEBUG("drm_process_request : %d", eDRMResult);
95                 return false;
96         }
97
98         return true;
99 #else
100         return false;
101 #endif
102 }
103
104 bool MsgDrmIsDrmFile(const char *szFilePath)
105 {
106 #if MSG_DRM_SUPPORT
107         drm_bool_type_e isDrm;
108         int eDRMResult = drm_is_drm_file(szFilePath, &isDrm);
109
110         if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
111                 MSG_DEBUG("file is not drm file");
112                 return false;
113         }
114
115         return true;
116 #else
117         return false;
118 #endif
119 }
120
121 /*Added to convert the .dm files in to .dcf files since our platform supports only .dcf :: Start*/
122 bool MsgDrmConvertDmtoDcfType(char *inputFile, char *outputFile)
123 {
124 #if MSG_DRM_SUPPORT
125         if ((NULL == inputFile) || (NULL == outputFile)) {
126                 MSG_DEBUG("Invalid Input parameters");
127                 return false;
128         }
129
130         if (strstr(inputFile, ".dm")) {
131                 MSG_SEC_DEBUG("Current File extension is .dm %s", inputFile);
132                 int ret;
133
134                 FILE *fp = MsgOpenFile(inputFile, "rb");/* Check fp */
135
136                 if (fp == NULL) {
137                         MSG_DEBUG("[File Open Fail(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
138                         strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
139                         return false;
140                 }
141
142                 if (MsgFseek(fp, 0L, SEEK_END) < 0) {
143                         MsgCloseFile(fp);
144                         MSG_DEBUG("MsgFseek() returns negative value!!!");
145                         return false;
146                 }
147                 long retVal = MsgFtell(fp);
148
149                 if (retVal < 0) {
150                         MsgCloseFile(fp);
151                         MSG_DEBUG("ftell() returns negative value: [%ld]!!!", retVal);
152                         strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
153                         return false;
154                 }
155
156                 unsigned long bufLen = retVal;
157                 MSG_DEBUG("fopen buffer len = %d", bufLen);
158                 if (MsgFseek(fp, 0, SEEK_SET) < 0) {
159                         MsgCloseFile(fp);
160                         MSG_DEBUG("MsgFseek() returns negative value!!!");
161                         return false;
162                 }
163
164                 unsigned char *buffer = (unsigned char*)malloc(bufLen);
165                 int readed_size = 0;
166                 int pathLen = strlen(inputFile);
167
168                 if (buffer == NULL) {
169                         MsgCloseFile(fp);
170                         MSG_DEBUG("malloc is failed ");
171                         strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
172                         return false;
173                 }
174
175                 strncpy(outputFile, inputFile, pathLen - 2);
176                 strncat(outputFile, "dcf", 3);
177
178                 readed_size = MsgReadFile(buffer, 1, bufLen, fp);/* Check for error */
179                 MSG_DEBUG("fread read size = %d", readed_size);
180                 if (readed_size == 0) {
181                         MsgCloseFile(fp);
182                         free(buffer);
183                         MSG_DEBUG("MsgReadFile returns 0");
184                         return false;
185                 }
186
187                 DRM_TRUSTED_CONVERT_HANDLE hConvert = NULL;
188                 drm_trusted_opn_conv_info_s trusted_open_conv_input;
189                 bzero(&trusted_open_conv_input, sizeof(drm_trusted_opn_conv_info_s));
190
191                 strncpy(trusted_open_conv_input.filePath, outputFile, DRM_TRUSTED_MAX_FILEPATH_LEN-1);
192                 trusted_open_conv_input.install_RO = DRM_TRUSTED_TRUE;
193
194                 ret = drm_trusted_open_convert(&trusted_open_conv_input, &hConvert);
195                 if (ret != DRM_RETURN_SUCCESS) {
196                         free(buffer);
197                         MsgCloseFile(fp);
198                         MSG_DEBUG("drm_trusted_open_convert() return = failed (0x%x)", ret);
199                         remove(outputFile);
200                         strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
201                         return false;
202                 }
203
204                 drm_trusted_write_conv_info_s trusted_write_conv_input;
205                 drm_trusted_write_conv_resp_s trusted_write_conv_output;
206
207                 bzero(&trusted_write_conv_input, sizeof(drm_trusted_write_conv_info_s));
208                 bzero(&trusted_write_conv_output, sizeof(drm_trusted_write_conv_resp_s));
209
210                 trusted_write_conv_input.data = buffer;
211                 trusted_write_conv_input.data_len = bufLen;
212
213                 /*We can call drm_trusted_write_convert in loop if file size is large*/
214                 ret = drm_trusted_write_convert(&trusted_write_conv_input, &trusted_write_conv_output, hConvert);
215                 if (ret != DRM_RETURN_SUCCESS) {
216                         free(buffer);
217                         MsgCloseFile(fp);
218                         MSG_DEBUG("drm_trusted_write_convert() return = failed (0x%x)", ret);
219                         remove(outputFile);
220                         strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
221                         return false;
222                 }
223
224                 ret = drm_trusted_close_convert(&hConvert);
225                 if (ret != DRM_RETURN_SUCCESS) {
226                         free(buffer);
227                         MsgCloseFile(fp);
228                         MSG_DEBUG("drm_trusted_close_convert() return = failed (0x%x)", ret);
229                         remove(outputFile);
230                         strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
231                         return false;
232                 }
233
234                 MsgCloseFile(fp);
235                 free(buffer);
236         } else {
237                 MSG_DEBUG("Current File extension is not .dm");
238
239                 MSG_DEBUG("inputFile = (%s)", inputFile);
240                 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
241
242                 return false;
243         }
244
245         return true;
246 #else
247         return true;
248 #endif
249 }
250
251 bool MsgDrmGetDrmType(const char *szFileName, MSG_DRM_TYPE *eDRMType)
252 {
253 #if MSG_DRM_SUPPORT
254         if (szFileName == NULL || eDRMType == NULL) {
255                 MSG_DEBUG("Param is NULL");
256                 return false;
257         }
258
259         drm_file_type_e file_type;
260         int result = drm_get_file_type(szFileName, &file_type);
261         if (result != DRM_RETURN_SUCCESS) {
262                 MSG_DEBUG("drm_get_file_type is failed %d", result);
263                 return false;
264         }
265
266         if (file_type == DRM_TYPE_OMA_V1) {
267                 drm_file_info_s drmInfo;
268                 bzero(&drmInfo, sizeof(drm_file_info_s));
269                 int eDRMResult = drm_get_file_info(szFileName, &drmInfo);
270                 if (DRM_RETURN_SUCCESS != eDRMResult) {
271                         MSG_DEBUG("drm_get_file_info is Fail eDRMResult = %d", eDRMResult);
272                         return false;
273                 }
274
275                 /* Convert DRM_METHOD into MSG_DRM_TYPE */
276                 switch (drmInfo.oma_info.method) {
277                 case DRM_METHOD_TYPE_FORWARD_LOCK:
278                         *eDRMType = MSG_DRM_FORWARD_LOCK;
279                         break;
280                 case DRM_METHOD_TYPE_COMBINED_DELIVERY:
281                         *eDRMType = MSG_DRM_COMBINED_DELIVERY;
282                         break;
283                 case DRM_METHOD_TYPE_SEPARATE_DELIVERY:
284                         *eDRMType = MSG_DRM_SEPARATE_DELIVERY;
285                         break;
286                 default:
287                         *eDRMType = MSG_DRM_NONE;
288                         break;
289                 }
290                 MSG_DEBUG("eDRMType : %d", *eDRMType);
291         } else {
292                 MSG_DEBUG("This is not a DRM_TYPE_OMA_V1 type");
293                 return false;
294         }
295
296         return true;
297 #else
298         return false;
299 #endif
300 }
301
302 bool MsgDrmGetMimeTypeEx(const char *szFileName, char *szMimeType, int nMimeTypeLen)
303 {
304 #if MSG_DRM_SUPPORT
305         if (!szFileName || !szMimeType || !nMimeTypeLen) {
306                 MSG_DEBUG("param is NULL");
307                 return false;
308         }
309
310         char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0, };
311
312         strncpy(strTemp, szFileName, strlen(szFileName));
313
314         drm_content_info_s tdcfContentinfo;
315         memset(&tdcfContentinfo, 0x00, sizeof(drm_content_info_s));
316         int eDRMResult = drm_get_content_info(strTemp, &tdcfContentinfo); /* Get attribute of DRM File */
317         if (DRM_RETURN_SUCCESS == eDRMResult) {
318                 MSG_DEBUG("contentType = %s", tdcfContentinfo.mime_type);
319                 snprintf(szMimeType, nMimeTypeLen, "%s", tdcfContentinfo.mime_type);
320
321         } else {
322                 MSG_DEBUG("drm_get_content_info is failed %d", eDRMResult);
323                 return false;
324         }
325
326         return true;
327 #else
328         return false;
329 #endif
330 }
331
332 bool MsgDrmGetContentID(const char *szFileName, char *szContentID, int nContentIDLen)
333 {
334 #if MSG_DRM_SUPPORT
335         if (!szFileName || !szContentID || !nContentIDLen) {
336                 MSG_DEBUG("param is NULL");
337                 return false;
338         }
339
340         char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0, };
341
342         strncpy(strTemp, szFileName, sizeof(strTemp)-1);
343
344         drm_content_info_s  content_info;
345         memset(&content_info, 0x00, sizeof(drm_content_info_s));
346
347         int result = drm_get_content_info(strTemp, &content_info);
348         if (DRM_RETURN_SUCCESS == result) {
349                 MSG_SEC_DEBUG("contentID = %s", content_info.content_id);
350         snprintf(szContentID, nContentIDLen, "%s", content_info.content_id);
351         } else {
352                 MSG_DEBUG("drm_get_content_info is failed %d", result);
353                 return false;
354         }
355
356         return true;
357 #else
358         return false;
359 #endif
360 }
361
362 bool MsgDrmCheckRingtone(const char *ringtonePath)
363 {
364 #if MSG_DRM_SUPPORT
365         bool ret = false;
366
367         if (ringtonePath) {
368                 drm_bool_type_e allowed = DRM_UNKNOWN;
369                 drm_action_allowed_data_s data;
370                 memset(&data, 0x00, sizeof(drm_action_allowed_data_s));
371                 strncpy(data.file_path, ringtonePath, strlen(ringtonePath));
372                 data.data = (int)DRM_SETAS_RINGTONE;
373
374                 int res = drm_is_action_allowed(DRM_HAS_VALID_SETAS_STATUS, &data, &allowed);
375
376                 if (res == DRM_RETURN_SUCCESS) {
377                         if (allowed == DRM_TRUE) {
378                                 MSG_DEBUG("allowed [DRM_TRUE]");
379                                 ret = true;
380                         }
381                 } else {
382                         MSG_DEBUG("fail to drm_is_action_allowed [0x%x]", res);
383                 }
384         } else {
385                 MSG_DEBUG("ringtonePath is NULL.");
386         }
387
388         return ret;
389 #else
390         return false;
391 #endif
392 }