summaryrefslogtreecommitdiff
path: root/utils/MsgDrmWrapper.cpp
blob: 80c4d3e549b771c1d5eb25265f71c769acf0d76d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include "MsgDebug.h"
#include "MsgDrmWrapper.h"
#include "MsgUtilFile.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/vfs.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#if MSG_DRM_SUPPORT

#include <drm_client_types.h>
#include <drm_client.h>

#endif /* MSG_DRM_SUPPORT */

#define MSG_MAX_DRM_FILE_PATH MSG_FILEPATH_LEN_MAX

bool MsgDrmRegisterFile(MSG_DRM_OPENMODE eMode, char *pBuffer, int nSize)
{
#if MSG_DRM_SUPPORT
	if (eMode == MSG_MODE_STREAM) {
		MSG_DEBUG("Fail(eMode == MSG_MODE_STREAM)");
		return false;
	}

	if (pBuffer == NULL) {
		MSG_DEBUG("[Error] pBuffer is NULL");
		return false;
	}

	MSG_DEBUG("buffer = %s, nSize = %d", pBuffer, nSize);

	drm_bool_type_e isDrm;
	int eDRMResult = drm_is_drm_file(pBuffer, &isDrm);

	if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
		MSG_DEBUG("file is not drm file");
		return false;
	}

	drm_request_type_e request_type = DRM_REQUEST_TYPE_REGISTER_FILE;

	eDRMResult = drm_process_request(request_type, pBuffer, NULL);
	if (DRM_RETURN_SUCCESS != eDRMResult) {
		MSG_DEBUG("drm_process_request is failed : 0x%x", eDRMResult);
		return false;
	}
	MSG_END();
	return true;
#else
	return false;
#endif
}

bool MsgDrmUnregisterFile(char *szFilename)
{
#if MSG_DRM_SUPPORT
	if (szFilename == NULL) {
		MSG_DEBUG("[Error] szFilename is NULL");
		return false;
	}

	MSG_DEBUG("szFilename = %s", szFilename);

	drm_request_type_e request_type = DRM_REQUEST_TYPE_UNREGISTER_FILE;

	int eDRMResult = drm_process_request(request_type, szFilename, NULL); /* Unregister a DCF file */
	if (DRM_RETURN_SUCCESS != eDRMResult) {
		MSG_DEBUG("drm_process_request : %d", eDRMResult);
		return false;
	}

	return true;
#else
	return false;
#endif
}

bool MsgDrmIsDrmFile(const char *szFilePath)
{
#if MSG_DRM_SUPPORT
	drm_bool_type_e isDrm;
	int eDRMResult = drm_is_drm_file(szFilePath, &isDrm);

	if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
		MSG_DEBUG("file is not drm file");
		return false;
	}

	return true;
#else
	return false;
#endif
}

/*Added to convert the .dm files in to .dcf files since our platform supports only .dcf :: Start*/
bool MsgDrmConvertDmtoDcfType(char *inputFile, char *outputFile)
{
#if MSG_DRM_SUPPORT
	if ((NULL == inputFile) || (NULL == outputFile)) {
		MSG_DEBUG("Invalid Input parameters");
		return false;
	}

	if (strstr(inputFile, ".dm")) {
		MSG_SEC_DEBUG("Current File extension is .dm %s", inputFile);
		int ret;

		FILE *fp = MsgOpenFile(inputFile, "rb");/* Check fp */

		if (fp == NULL) {
			MSG_DEBUG("[File Open Fail(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
			strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
			return false;
		}

		if (MsgFseek(fp, 0L, SEEK_END) < 0) {
			MsgCloseFile(fp);
			MSG_DEBUG("MsgFseek() returns negative value!!!");
			return false;
		}
		long retVal = MsgFtell(fp);

		if (retVal < 0) {
			MsgCloseFile(fp);
			MSG_DEBUG("ftell() returns negative value: [%ld]!!!", retVal);
			strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
			return false;
		}

		unsigned long bufLen = retVal;
		MSG_DEBUG("fopen buffer len = %d", bufLen);
		if (MsgFseek(fp, 0, SEEK_SET) < 0) {
			MsgCloseFile(fp);
			MSG_DEBUG("MsgFseek() returns negative value!!!");
			return false;
		}

		unsigned char *buffer = (unsigned char*)malloc(bufLen);
		int readed_size = 0;
		int pathLen = strlen(inputFile);

		if (buffer == NULL) {
			MsgCloseFile(fp);
			MSG_DEBUG("malloc is failed ");
			strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
			return false;
		}

		strncpy(outputFile, inputFile, pathLen - 2);
		strncat(outputFile, "dcf", 3);

		readed_size = MsgReadFile(buffer, 1, bufLen, fp);/* Check for error */
		MSG_DEBUG("fread read size = %d", readed_size);
		if (readed_size == 0) {
			MsgCloseFile(fp);
			free(buffer);
			MSG_DEBUG("MsgReadFile returns 0");
			return false;
		}

		DRM_TRUSTED_CONVERT_HANDLE hConvert = NULL;
		drm_trusted_opn_conv_info_s trusted_open_conv_input;
		bzero(&trusted_open_conv_input, sizeof(drm_trusted_opn_conv_info_s));

		strncpy(trusted_open_conv_input.filePath, outputFile, DRM_TRUSTED_MAX_FILEPATH_LEN-1);
		trusted_open_conv_input.install_RO = DRM_TRUSTED_TRUE;

		ret = drm_trusted_open_convert(&trusted_open_conv_input, &hConvert);
		if (ret != DRM_RETURN_SUCCESS) {
			free(buffer);
			MsgCloseFile(fp);
			MSG_DEBUG("drm_trusted_open_convert() return = failed (0x%x)", ret);
			remove(outputFile);
			strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
			return false;
		}

		drm_trusted_write_conv_info_s trusted_write_conv_input;
		drm_trusted_write_conv_resp_s trusted_write_conv_output;

		bzero(&trusted_write_conv_input, sizeof(drm_trusted_write_conv_info_s));
		bzero(&trusted_write_conv_output, sizeof(drm_trusted_write_conv_resp_s));

		trusted_write_conv_input.data = buffer;
		trusted_write_conv_input.data_len = bufLen;

		/*We can call drm_trusted_write_convert in loop if file size is large*/
		ret = drm_trusted_write_convert(&trusted_write_conv_input, &trusted_write_conv_output, hConvert);
		if (ret != DRM_RETURN_SUCCESS) {
			free(buffer);
			MsgCloseFile(fp);
			MSG_DEBUG("drm_trusted_write_convert() return = failed (0x%x)", ret);
			remove(outputFile);
			strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
			return false;
		}

		ret = drm_trusted_close_convert(&hConvert);
		if (ret != DRM_RETURN_SUCCESS) {
			free(buffer);
			MsgCloseFile(fp);
			MSG_DEBUG("drm_trusted_close_convert() return = failed (0x%x)", ret);
			remove(outputFile);
			strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
			return false;
		}

		MsgCloseFile(fp);
		free(buffer);
	} else {
		MSG_DEBUG("Current File extension is not .dm");

		MSG_DEBUG("inputFile = (%s)", inputFile);
		strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);

		return false;
	}

	return true;
#else
	return true;
#endif
}

bool MsgDrmGetDrmType(const char *szFileName, MSG_DRM_TYPE *eDRMType)
{
#if MSG_DRM_SUPPORT
	if (szFileName == NULL || eDRMType == NULL) {
		MSG_DEBUG("Param is NULL");
		return false;
	}

	drm_file_type_e file_type;
	int result = drm_get_file_type(szFileName, &file_type);
	if (result != DRM_RETURN_SUCCESS) {
		MSG_DEBUG("drm_get_file_type is failed %d", result);
		return false;
	}

	if (file_type == DRM_TYPE_OMA_V1) {
		drm_file_info_s drmInfo;
		bzero(&drmInfo, sizeof(drm_file_info_s));
		int eDRMResult = drm_get_file_info(szFileName, &drmInfo);
		if (DRM_RETURN_SUCCESS != eDRMResult) {
			MSG_DEBUG("drm_get_file_info is Fail eDRMResult = %d", eDRMResult);
			return false;
		}

		/* Convert DRM_METHOD into MSG_DRM_TYPE */
		switch (drmInfo.oma_info.method) {
		case DRM_METHOD_TYPE_FORWARD_LOCK:
			*eDRMType = MSG_DRM_FORWARD_LOCK;
			break;
		case DRM_METHOD_TYPE_COMBINED_DELIVERY:
			*eDRMType = MSG_DRM_COMBINED_DELIVERY;
			break;
		case DRM_METHOD_TYPE_SEPARATE_DELIVERY:
			*eDRMType = MSG_DRM_SEPARATE_DELIVERY;
			break;
		default:
			*eDRMType = MSG_DRM_NONE;
			break;
		}
		MSG_DEBUG("eDRMType : %d", *eDRMType);
	} else {
		MSG_DEBUG("This is not a DRM_TYPE_OMA_V1 type");
		return false;
	}

	return true;
#else
	return false;
#endif
}

bool MsgDrmGetMimeTypeEx(const char *szFileName, char *szMimeType, int nMimeTypeLen)
{
#if MSG_DRM_SUPPORT
	if (!szFileName || !szMimeType || !nMimeTypeLen) {
		MSG_DEBUG("param is NULL");
		return false;
	}

	char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0, };

	strncpy(strTemp, szFileName, strlen(szFileName));

	drm_content_info_s tdcfContentinfo;
	memset(&tdcfContentinfo, 0x00, sizeof(drm_content_info_s));
	int eDRMResult = drm_get_content_info(strTemp, &tdcfContentinfo); /* Get attribute of DRM File */
	if (DRM_RETURN_SUCCESS == eDRMResult) {
		MSG_DEBUG("contentType = %s", tdcfContentinfo.mime_type);
		snprintf(szMimeType, nMimeTypeLen, "%s", tdcfContentinfo.mime_type);

	} else {
		MSG_DEBUG("drm_get_content_info is failed %d", eDRMResult);
		return false;
	}

	return true;
#else
	return false;
#endif
}

bool MsgDrmGetContentID(const char *szFileName, char *szContentID, int nContentIDLen)
{
#if MSG_DRM_SUPPORT
	if (!szFileName || !szContentID || !nContentIDLen) {
		MSG_DEBUG("param is NULL");
		return false;
	}

	char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0, };

	strncpy(strTemp, szFileName, sizeof(strTemp)-1);

	drm_content_info_s  content_info;
	memset(&content_info, 0x00, sizeof(drm_content_info_s));

	int result = drm_get_content_info(strTemp, &content_info);
	if (DRM_RETURN_SUCCESS == result) {
		MSG_SEC_DEBUG("contentID = %s", content_info.content_id);
        snprintf(szContentID, nContentIDLen, "%s", content_info.content_id);
	} else {
		MSG_DEBUG("drm_get_content_info is failed %d", result);
		return false;
	}

	return true;
#else
	return false;
#endif
}

bool MsgDrmCheckRingtone(const char *ringtonePath)
{
#if MSG_DRM_SUPPORT
	bool ret = false;

	if (ringtonePath) {
		drm_bool_type_e allowed = DRM_UNKNOWN;
		drm_action_allowed_data_s data;
		memset(&data, 0x00, sizeof(drm_action_allowed_data_s));
		strncpy(data.file_path, ringtonePath, strlen(ringtonePath));
		data.data = (int)DRM_SETAS_RINGTONE;

		int res = drm_is_action_allowed(DRM_HAS_VALID_SETAS_STATUS, &data, &allowed);

		if (res == DRM_RETURN_SUCCESS) {
			if (allowed == DRM_TRUE) {
				MSG_DEBUG("allowed [DRM_TRUE]");
				ret = true;
			}
		} else {
			MSG_DEBUG("fail to drm_is_action_allowed [0x%x]", res);
		}
	} else {
		MSG_DEBUG("ringtonePath is NULL.");
	}

	return ret;
#else
	return false;
#endif
}