7b595f32ccddcb5489f8ca64d28c91f8918ccc35
[platform/core/security/drm-service-core-tizen.git] / tappsd / src / rights / DTapps2Rights.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
17 /**
18  * @file        DTapps2Rights.cpp
19  * @brief       This file includes functions relating to Rights.
20  */
21
22 #include <string>
23
24 #include "TADC_Core.h"
25 #include "TADC_IF.h"
26 #include "drm-tizen-error.h"
27
28 #include "TADC_Util.h"
29
30 #include "DTapps2Rights.h"
31 #include "DTapps2SqliteDB.h"
32 #include "DTapps2HMAC.h"
33 #include "DTapps2Base64.h"
34 #include "DTapps2Time.h"
35
36 /* Validation and Installation of the Constraints Structure */
37 int DTappsValidateConstraints(DTAPPS_CONSTRAINTS* st_const, DTAPPS_OPERATION opr)
38 {
39         int ret = TADC_LICENSE_VALID;
40         char DUID[DTAPPS_DUID_SIZE] = {0, };
41         unsigned char zeroduid[DTAPPS_DUID_SIZE] = {0, };
42         int retDeviceInfo = 0;
43
44         if (st_const == NULL)
45         {
46                 DRM_TAPPS_EXCEPTION("Invalid Paramter st_const = %p", st_const);
47                 ret = TADC_PARAMETER_ERROR;
48                 goto Error_Exit;
49         }
50
51         /* Check for DUID */
52         if (DTAPPS_MEMCMP(st_const->DUID, zeroduid, DTAPPS_DUID_SIZE))
53         {
54                 DRM_TAPPS_LOG("st_const->DUID = %s", st_const->DUID);
55                 if(opr == DTAPPS_VERIFY || opr == DTAPPS_INSTALL)
56                 {
57                         retDeviceInfo = TADC_IF_GetDUID(DUID);
58                         if (retDeviceInfo < 0)
59                         {
60                                 DRM_TAPPS_EXCEPTION("Retrieve for DUID Failed!!! DUID = %s, duid_len = %d", DUID, strlen(DUID));
61                                 return TADC_LICENSE_UNKNOWN_ERROR;
62                         }
63                         if (!(DTAPPS_MEMCMP(st_const->DUID, DUID, DTAPPS_DUID_SIZE)))
64                         {
65                                 DRM_TAPPS_LOG("DUID match!! st_const->DUID = %s, DUID = %s", st_const->DUID, DUID);
66                                 ret = TADC_LICENSE_VALID;
67                         }
68                         else
69                         {
70                                 DRM_TAPPS_EXCEPTION("DUID don't match!!! st_const->DUID = %s, DUID = %s", st_const->DUID, DUID);
71                                 ret = TADC_LICENSE_DUID_MISMATCH;
72                                 goto Error_Exit;
73                         }
74                 }
75         }
76         DRM_TAPPS_LOG("ret = %d", ret);
77         return ret;
78
79 Error_Exit:
80         DRM_TAPPS_EXCEPTION("Validate Constraints failed!!!, ret = %d", ret);
81         return ret;
82 }
83
84 int DTappsInstallLicense(const char* declicbuffer)
85 {
86         unsigned char* db_buffer_org = NULL;
87         unsigned char db_buffer_enc[512] = {0, };
88         int len_enc = 0, check_ret = 0;
89         unsigned char *pDevKey = NULL;
90         unsigned int DevKeyLen = 0;
91         unsigned char hash[32] = {0, };
92         unsigned char hash_enc[64] = {0, };
93         unsigned int hash_len = 0;
94         int check = TADC_LICENSE_VALID;
95
96         BOOL isOk = FALSE;
97         int ret = TADC_SUCCESS;
98
99         unsigned char name[DTAPPS_NAME_SIZE] = {0, };
100         unsigned char nullCek[CEK_SIZE] = {0, };
101
102         int len = 0;
103         time_t now = 0;
104         char time_buffer[21] = {0, };
105         T_RO t_RO;
106
107         memset(&t_RO, 0x00, sizeof(t_RO));
108
109         DTAPPS_RIGHTS_ROW row;
110
111 #ifdef DTAPPS_STORE_CEK_IN_DB
112         AES_KEY key;
113
114         memset(&key, 0x00, sizeof(key));
115
116         unsigned char *encr_cek = NULL;
117         unsigned int keylen = 0;
118         unsigned int encr_cek_len = 0;
119         unsigned char b64_encr_cek[128] = {0, };
120         unsigned char encr_cek_hash[32] = {0, };
121         unsigned char b64_encr_cek_hash[64] = {0, };
122         unsigned int encr_cek_hash_len = 0;
123 #endif
124
125         // Copy necessary fields into structure to be stored in DB
126         DTAPPS_CONSTRAINTS st_constraints = {0, };
127
128         // First Parse the decrypted Licence buffer in order to fill the t_RO buffer and use it to store in DB
129         check_ret = DTappsGetROInfo(declicbuffer, &t_RO, name);
130         if (check_ret < 0)
131         {
132                 DRM_TAPPS_EXCEPTION("DTappsGetROInfo Error Code = %x, check_ret = %d", TADC_LICENSEXMLPARSING_ERROR,check_ret);
133                 ret = TADC_LICENSEXMLPARSING_ERROR;
134                 goto Error_Exit;
135         }
136
137         /* Copy DUID */
138         if (t_RO.t_Permission.t_Individual.DUID != NULL)
139         {
140                 DTAPPS_MEMCPY(st_constraints.DUID, t_RO.t_Permission.t_Individual.DUID, TAPPS_STRLEN((const char*)t_RO.t_Permission.t_Individual.DUID));
141         }
142         DRM_TAPPS_LOG("st_constraints.DUID = %s", st_constraints.DUID);
143
144         /* Validation of constraints */
145         check = DTappsValidateConstraints(&st_constraints, DTAPPS_INSTALL);
146         if (check != TADC_LICENSE_VALID)
147         {
148                 DRM_TAPPS_LOG("Constraints obtained are NOT VALID!!!, check = %d",check);
149                 ret = TADC_LICENSE_INVALID_ERROR;
150                 goto Error_Exit;
151         }
152
153         /* Create the contraints buffer to be put in DB */
154         db_buffer_org = (unsigned char*)DTAPPS_MALLOC(sizeof(DTAPPS_CONSTRAINTS));
155         if (db_buffer_org == NULL)
156         {
157                 DRM_TAPPS_EXCEPTION("Memory Allocation error , db_buffer_org = %p", db_buffer_org);
158                 ret = TADC_MEMAlOC_ERROR;
159                 goto Error_Exit;
160         }
161
162         DTAPPS_MEMSET(db_buffer_org, 0x00, sizeof(DTAPPS_CONSTRAINTS));
163         DTAPPS_MEMCPY(db_buffer_org, &st_constraints, sizeof(DTAPPS_CONSTRAINTS));
164
165         /* Calculate the hash - HMAC for the constraints buffer */
166         if (0 == DTappsGetDeviceKey(&pDevKey, &DevKeyLen))
167         {
168                 DRM_TAPPS_EXCEPTION("Error in DTAppsGetDeviceKey!!!, pDevKey = %p, DevKeyLen = %d", pDevKey, DevKeyLen);
169                 ret = TADC_GET_DEVICEKEY_ERROR;
170                 goto Error_Exit;
171         }
172
173         DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, db_buffer_org, sizeof(DTAPPS_CONSTRAINTS), hash, &hash_len);
174
175         /* Encode the buffer and hash using Base64 and then store it into the DB */
176         /* Base64 Encode the constraints buffer */
177         len_enc = (sizeof(DTAPPS_CONSTRAINTS) + 2) / 3 * 4;
178
179         isOk = DTappsB64Encode(db_buffer_org, sizeof(DTAPPS_CONSTRAINTS), db_buffer_enc, len_enc);
180         if (isOk != TRUE)
181         {
182                 DRM_TAPPS_EXCEPTION("BASE64 Encoding failure!!!! ret = %d", isOk);
183                 ret = TADC_GET_BASE64ENCODE_ERROR;
184                 goto Error_Exit;
185         }
186
187         /* Base64 Encode the hash buffer */
188         len_enc = (hash_len + 2) / 3 * 4;
189
190         isOk = DTappsB64Encode(hash, hash_len, hash_enc, len_enc);
191         if (isOk != TRUE)
192         {
193                 DRM_TAPPS_EXCEPTION("BASE64 Encoding failure!!!! ret = %d", isOk);
194                 ret = TADC_GET_BASE64ENCODE_ERROR;
195                 goto Error_Exit;
196         }
197
198         DRM_TAPPS_LOG("CEK installation started!!");
199         if (DTAPPS_MEMCMP((void*)t_RO.t_Content.CEK, (void*)nullCek, CEK_SIZE) != 0)
200         {
201 #ifdef DTAPPS_STORE_CEK_IN_DB
202                 /* Store the CEK in DB */
203                 /* First encrypt the CEK , calculate the HASH and then convert both into BASE64 Encoded form */
204                 //ceklen = (unsigned int)TAPPS_STRLEN((const char*) t_RO.t_Content.CEK);
205                 /* keylen => Should be a multiple of 8 */
206                 keylen = CEK_SIZE;
207                 encr_cek_len = keylen + 8;
208
209                 if ((check_ret = DTAPPS_AES_SET_ENCR_KEY(pDevKey, 128, &key)) != 0)
210                 {
211                         DRM_TAPPS_EXCEPTION("DTAPPS_AES_SET_ENC_KEY failed!!, check_ret = %d", check_ret);
212                         ret =  TADC_SET_AES_ENCRYPTKEY_ERROR;
213                         goto Error_Exit;
214                 }
215
216                 encr_cek = (unsigned char*)DTAPPS_MALLOC(encr_cek_len);
217                 if (NULL == encr_cek)
218                 {
219                         DRM_TAPPS_EXCEPTION("Memory Allocation Error!!, encr_cek = %p", encr_cek);
220                         ret = TADC_MEMAlOC_ERROR;
221                         goto Error_Exit;
222                 }
223
224                 check_ret = DTAPPS_AES_WRAP_KEY(&key, NULL, encr_cek, t_RO.t_Content.CEK, keylen);
225                 if (check_ret <= 0)
226                 {
227                         DRM_TAPPS_EXCEPTION("DTAPPS_AES_WRAP_KEY failed!!, check_ret = %d", check_ret);
228                         ret = TADC_SET_AES_WRAPKEY_ERROR;
229                         goto Error_Exit;
230                 }
231                 DRM_TAPPS_LOG("encr_cek = %s", encr_cek);
232
233                 /* Calculate the HASH for Encrypted CEK */
234                 DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, encr_cek, encr_cek_len, encr_cek_hash, &encr_cek_hash_len);
235                 DRM_TAPPS_LOG("encr_cek_hash = %s", encr_cek_hash);
236
237                 /* Base64 Encode the Encrypted CEK */
238                 len_enc = (encr_cek_len + 2) / 3 * 4;
239                 isOk = DTappsB64Encode(encr_cek, encr_cek_len, b64_encr_cek, len_enc);
240                 if (isOk != TRUE)
241                 {
242                         DRM_TAPPS_EXCEPTION("BASE64 Encoding failure!!!! ret = %d", isOk);
243                         ret = TADC_GET_BASE64ENCODE_ERROR;
244                         goto Error_Exit;
245                 }
246
247                 /* Base64 Encode the Hash of Encrypted CEK */
248                 len_enc = (encr_cek_hash_len + 2) / 3 * 4;
249                 isOk = DTappsB64Encode(encr_cek_hash, encr_cek_hash_len, b64_encr_cek_hash, len_enc);
250                 if (isOk != TRUE)
251                 {
252                         DRM_TAPPS_EXCEPTION("BASE64 Encoding failure!!!! ret = %d", isOk);
253                         ret = TADC_GET_BASE64ENCODE_ERROR;
254                         goto Error_Exit;
255                 }
256
257                 DRM_TAPPS_LOG("b64_encr_cek = %s, b64_encr_cek_hash = %s", b64_encr_cek, b64_encr_cek_hash);
258 #else
259                 DRM_TAPPS_LOG("CEK in Secure Storage to be implemented!!!");
260 #endif
261         }
262         else
263         {
264                 DRM_TAPPS_EXCEPTION("t_RO.t_Content.CEK is null!!");
265         }
266
267         /* Calculate the Time of storage */
268         now = DTAPPS_TIME(NULL);
269         struct tm time_gmt;
270     DTAPPS_GMTIME_THREAD_SAFE(&now, &time_gmt);
271
272         /* Format the time to be stored in the DB buffer - DATETIME format */
273         len = DTAPPS_SNPRINTF(
274                         time_buffer,
275                         sizeof(time_buffer),
276                         "%04d-%02d-%02dT%02d:%02d:%02dZ",
277                                 time_gmt.tm_year + 1900,
278                                 time_gmt.tm_mon + 1,
279                                 time_gmt.tm_mday,
280                                 time_gmt.tm_hour,
281                                 time_gmt.tm_min,
282                                 time_gmt.tm_sec);
283
284         if ((len == -1) || (len >= (int)sizeof(time_buffer)))
285         {
286                 DRM_TAPPS_EXCEPTION("snprintf failed , len = %d, sizeof(time_buffer) = %d",len,sizeof(time_buffer));
287                 ret = TADC_MEMAlOC_ERROR;
288                 goto Error_Exit;
289         }
290         DRM_TAPPS_LOG("time_buffer = %s", time_buffer);
291         DRM_TAPPS_LOG("Constraint_buffer = %s, Constraint_Hash = %s", db_buffer_enc, hash_enc);
292
293         // 2013/04/25 added for db update
294         isOk = DTapps_RIGHTS_SELECT_ONE((char*) t_RO.t_Content.CID, &row);
295
296         // if there is no rights in the db, insert into rights to db
297         if (isOk != TRUE)
298         {
299                 DRM_TAPPS_LOG("Insert into dtapps2rights started....");
300         isOk = DTapps_RIGHTS_INSTALL((char*) name, time_buffer,
301                                                                         #ifdef DTAPPS_STORE_CEK_IN_DB
302                                                                         (char*) b64_encr_cek, (char*) b64_encr_cek_hash,
303                                                                         #endif
304                                                                         (char*) db_buffer_enc, (char*) hash_enc, (char*) t_RO.t_Content.CID,
305                                                                         FALSE);
306             if (isOk != TRUE)
307                 {
308                         DRM_TAPPS_EXCEPTION("DB Installation failure!!!! ret = %d", isOk);
309                         ret = TADC_DB_INSTALL_ERROR;
310                         goto Error_Exit;
311                 }
312                 DRM_TAPPS_LOG("DrmTdcInstallLicense Success!!!!!! ret = %d", ret);
313         }
314         // if there is same cid rights exist, update right to db
315         else
316         {
317                 DRM_TAPPS_LOG("Update dtapps2rights started....");
318                 isOk = DTapps_RIGHTS_INSTALL((char*) name, time_buffer,
319                                                                         #ifdef DTAPPS_STORE_CEK_IN_DB
320                                                                         (char*) b64_encr_cek, (char*) b64_encr_cek_hash,
321                                                                         #endif
322                                                                         (char*) db_buffer_enc, (char*) hash_enc, (char*) t_RO.t_Content.CID,
323                                                                         TRUE);
324             if (isOk != TRUE)
325                 {
326                         DRM_TAPPS_EXCEPTION("DB Update failure!!!! ret = %d", isOk);
327                         ret = TADC_DB_INSTALL_ERROR;
328                         goto Error_Exit;
329                 }
330                 DRM_TAPPS_LOG("DrmTdcInstallLicense Success!!!!!! ret = %d", ret);
331         }
332
333         // Test Code for get the plaintext ro
334         /*{
335             std::string tmpPath((char *)(t_RO.t_Content.CID));
336             tmpPath += ".ro";
337
338                 FILE *fd = fopen(tmpPath.c_str(), "w+b");
339                 if (fd == NULL)
340                 {
341                         goto Error_Exit;
342                 }
343                 fwrite(declicbuffer, 1, strlen(declicbuffer), fd);
344                 fclose(fd);
345                 DRM_TAPPS_LOG("Save decrypted RO success!, path = %s", tmpPath.c_str());
346         }*/
347         // Test Code Done!
348
349 Error_Exit:
350         TADC_MEMFree_RO(&t_RO);
351
352         if (db_buffer_org)
353         {
354                 DTAPPS_FREE((unsigned char*)db_buffer_org);
355         }
356         if (encr_cek)
357         {
358                 DTAPPS_FREE(encr_cek);
359         }
360
361         return ret;
362 }
363
364 int DTappsHasValidLicense(const char* szCid)
365 {
366         int const_buf_enclen = 0, hash_buf_enclen = 0, const_buf_declen = 0, hash_buf_declen = 0;
367
368         unsigned char *pDevKey = NULL;
369         unsigned int DevKeyLen = 0;
370         unsigned char const_buffer_dec[512] = {0, };
371         unsigned char hash_buffer_dec[64] = {0, };
372         unsigned char md[64] = {0, };
373         unsigned int md_len = 0;
374
375         DTAPPS_RIGHTS_ROW row;
376         bool check = FALSE;
377
378         int check_valid = TADC_LICENSE_VALID;
379         DTAPPS_CONSTRAINTS st_constaints = {0, };
380
381         check = DTapps_RIGHTS_SELECT_ONE(szCid, &row);
382         if (check != TRUE)
383         {
384                 DRM_TAPPS_EXCEPTION("There is no RO in the DB. cid  = %s", szCid);
385                 check_valid = TADC_LICENSE_NO_LICENSE;
386                 goto DTAPPS_END;
387         }
388
389         if (0 == DTappsGetDeviceKey(&pDevKey, &DevKeyLen))
390         {
391                 DRM_TAPPS_EXCEPTION("Error in DTAppsGetDeviceKey pDevKey = %s, DevKeyLen = %d", pDevKey, DevKeyLen);
392                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
393                 goto DTAPPS_END;
394         }
395
396         /* BASE64 Decode the constraints buffer extracted from the DB */
397         const_buf_enclen = TAPPS_STRLEN(row.constraint_buffer);
398         const_buf_declen = const_buf_enclen / 4 * 3;
399         if (const_buf_enclen <= 0)
400         {
401                 DRM_TAPPS_EXCEPTION("There is no constaints buffer in the DB. cid = %s", szCid);
402                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
403                 goto DTAPPS_END;
404         }
405         DRM_TAPPS_FRQ_LOG("const_buf_enclen = %d, const_buf_declen = %d", const_buf_enclen, const_buf_declen);
406
407         (void)DTappsB64Decode((unsigned char*)row.constraint_buffer, const_buf_enclen, const_buffer_dec, const_buf_declen);
408         if (const_buf_declen > (const_buf_enclen / 4 * 3))
409         {
410                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! const_buffer_dec = %s, const_buf_declen = %d, (const_buf_enclen/4*3) = %d", const_buffer_dec, const_buf_declen, (const_buf_enclen / 4 * 3));
411                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
412                 goto DTAPPS_END;
413         }
414
415         /* BASE64 Decode the hash buffer extracted from the DB */
416         DTAPPS_MEMSET(hash_buffer_dec, 0x00, sizeof(hash_buffer_dec));
417         DTAPPS_MEMSET(md, 0x00, sizeof(md));
418
419         hash_buf_enclen = TAPPS_STRLEN(row.constraint_hash);
420         hash_buf_declen = hash_buf_enclen / 4 * 3;
421         if (hash_buf_enclen <= 0)
422         {
423                 DRM_TAPPS_EXCEPTION("There is no constaints_hash buffer in the DB. cid = %s", szCid);
424                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
425                 goto DTAPPS_END;
426         }
427         DRM_TAPPS_FRQ_LOG("hash_buf_enclen = %d, hash_buf_declen = %d", hash_buf_enclen, hash_buf_declen);
428
429         (void)DTappsB64Decode((unsigned char*)row.constraint_hash, hash_buf_enclen, hash_buffer_dec, hash_buf_declen);
430         if (hash_buf_declen > (hash_buf_enclen / 4 * 3))
431         {
432                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! hash_buffer_dec = %s, hash_buf_declen = %d, (hash_buf_enclen/4*3) = %d", hash_buffer_dec, hash_buf_declen, (hash_buf_enclen / 4 * 3));
433                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
434                 goto DTAPPS_END;
435         }
436
437         /* Calculate the hash using HMAC from the BASE64Decoded Constraint Buffers and check with the BASE64Decoded Hash buffers */
438         DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, const_buffer_dec, const_buf_declen, md, &md_len);
439         if (DTAPPS_MEMCMP(md, hash_buffer_dec, hash_buf_declen) == 0)
440         {
441                 DRM_TAPPS_LOG("HASH matching!!! md = %s, hash_buffer_dec = %s, hash_buf_declen = %d", md, hash_buffer_dec, hash_buf_declen);
442         }
443         else
444         {
445                 DRM_TAPPS_EXCEPTION("HASH not matching!!! md = %s, hash_buffer_dec = %s, hash_buf_declen = %d", md, hash_buffer_dec, hash_buf_declen);
446                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
447                 goto DTAPPS_END;
448         }
449
450         /* Validate the constraints buffer */
451         DTAPPS_MEMCPY(&st_constaints, const_buffer_dec, sizeof(DTAPPS_CONSTRAINTS));
452
453         check_valid = DTappsValidateConstraints(&st_constaints, DTAPPS_VERIFY);
454         if (check_valid != TADC_LICENSE_VALID)
455         {
456                 DRM_TAPPS_LOG("Invalid Constraints for constraints_buffer = %s check_valid = %d", const_buffer_dec, check_valid);
457                 /* TODO: Implementation of Auto Deletion feature to delete the rights from the DB if the rights are expired!!!*/
458                 goto DTAPPS_END;
459         }
460         else
461         {
462                 DRM_TAPPS_LOG("Rights are Valid!!! Found atleast one Valid Rights..... return with Success!! check_valid = %d", check_valid);
463         }
464
465 DTAPPS_END:
466         if (pDevKey)
467         {
468                 DTappsFreeDeviceKey(&pDevKey);
469         }
470         if (check_valid != TADC_LICENSE_VALID)
471         {
472                 DRM_TAPPS_EXCEPTION("Constraints not Valid!!!! check_valid = %d", check_valid);
473                 DRM_TAPPS_EXCEPTION("DrmTdcHasValidLicense Failed!!!");
474                 return check_valid;
475         }
476         else
477         {
478                 DRM_TAPPS_LOG("Constraints are Valid!! check_valid = %d", check_valid);
479                 return TADC_LICENSE_VALID;
480         }
481 }
482
483 int DTappsGetROInfo(const char* pszXML, T_RO *t_RO, unsigned char* name)
484 {
485         int                             nResult = 0;
486         CXMLFile                oXMLFile;
487         CXMLElement*    pRoot, *pElement;
488         CPointerArray   paChilds;
489         LPCTSTR                 pszValue;
490         int                             length = 0;
491         unsigned char   *pbBuffer = NULL;
492         int len_enc = 0, len_dec = 0;
493
494         const char *pRoHeader = "<?xml version=\"1.0\"?>\n<TizenLicense>";
495
496         //Check Param Buffer
497         if (pszXML == NULL || t_RO == NULL || name == NULL)
498         {
499                 DRM_TAPPS_EXCEPTION("Parameters NULL!!! pszXML = %p, t_RO = %p", pszXML, t_RO);
500                 return -1;
501         }
502
503         if (memcmp(pszXML, pRoHeader, strlen(pRoHeader))) {
504                 DRM_TAPPS_EXCEPTION("TADC_IF_MemCmp. pRoHeader[%s] Error Code = %x", pRoHeader, TADC_XMLPARSER_ERROR);
505                 return -1;
506         }
507
508         DTAPPS_MEMSET(t_RO, 0, sizeof(T_RO));
509
510         nResult = oXMLFile.LoadFromStream((LPCTSTR)pszXML);
511         if (nResult < 0)
512         {
513                 DRM_TAPPS_EXCEPTION("oXMLFile.LoadFromStream is failed\n");
514                 nResult = -1;
515                 goto finish;
516         }
517
518         // Set Version
519         pRoot = oXMLFile.GetRoot();
520         if (pRoot == NULL)
521         {
522                 DRM_TAPPS_EXCEPTION("oXMLFile.GetRoot is failed\n");
523                 nResult = -1;
524                 goto finish;
525         }
526
527         // Get UID -> Name
528         pRoot = oXMLFile.GetRoot();
529         if (pRoot == NULL)
530         {
531                 DRM_TAPPS_EXCEPTION("oXMLFile.GetRoot is failed\n");
532                 nResult = -1;
533                 goto finish;
534         }
535
536         paChilds.RemoveAll();
537         nResult = pRoot->Find(&paChilds, _T("LicenseInfo"), _T("uid"), NULL);
538         if (nResult != 0)
539         {
540                 DRM_TAPPS_EXCEPTION("pRoot->Find is failed\n");
541                 nResult = -1;
542                 goto finish;
543         }
544
545         if (paChilds.GetCount() != 1)
546         {
547                 DRM_TAPPS_EXCEPTION("paChilds.GetCount() is failed\n");
548                 nResult = -1;
549                 goto finish;
550         }
551
552         pElement = (CXMLElement*) paChilds.Get(0);
553         pszValue = pElement->GetValue();
554         if (pszValue == NULL)
555         {
556                 DRM_TAPPS_EXCEPTION("pElement->GetValue() is failed\n");
557                 nResult = -2;
558                 goto finish;
559         }
560
561         length = TAPPS_STRLEN(pszValue);
562         if (length <= 0 || length > DTAPPS_NAME_SIZE)
563         {
564                 DRM_TAPPS_EXCEPTION("TAPPS_STRLEN failed(%d) -  %s\n", length, pszValue);
565                 nResult = -1;
566                 goto finish;
567         }
568
569         memcpy(name, pszValue, length + 1);
570         DRM_TAPPS_LOG("name = %s", name);
571
572         // Get UID -> CID
573         pRoot = oXMLFile.GetRoot();
574         if (pRoot == NULL)
575         {
576                 DRM_TAPPS_EXCEPTION("oXMLFile.GetRoot is failed\n");
577                 nResult = -1;
578                 goto finish;
579         }
580
581         paChilds.RemoveAll();
582         nResult = pRoot->Find(&paChilds, _T("ContentInfo"), _T("cid"), NULL);
583         if (nResult != 0)
584         {
585                 DRM_TAPPS_EXCEPTION("pRoot->Find is failed\n");
586                 nResult = -1;
587                 goto finish;
588         }
589         if (paChilds.GetCount() != 1)
590         {
591                 DRM_TAPPS_EXCEPTION("paChilds.GetCount() is failed\n");
592                 nResult = -1;
593                 goto finish;
594         }
595
596         pElement = (CXMLElement*)paChilds.Get(0);
597         pszValue = pElement->GetValue();
598         if (pszValue == NULL)
599         {
600                 DRM_TAPPS_EXCEPTION("pElement->GetValue() is failed\n");
601                 nResult = -2;
602                 goto finish;
603         }
604
605         length = TAPPS_STRLEN(pszValue);
606         if (length <= 0 || length > CID_SIZE)
607         {
608                 DRM_TAPPS_EXCEPTION("TAPPS_STRLEN is failed(%d) -  %s\n", length, pszValue);
609                 nResult = -1;
610                 goto finish;
611         }
612
613         t_RO->t_Content.CID = (TADC_U8*)DTAPPS_MALLOC(CID_SIZE + 1);
614         IF_TRUE_GOTO(t_RO->t_Content.CID == NULL, -2);
615
616         memset(t_RO->t_Content.CID, 0, CID_SIZE + 1);
617         memcpy(t_RO->t_Content.CID, pszValue, CID_SIZE + 1);
618
619         // Get KeyValue (CEK)
620         pRoot = oXMLFile.GetRoot();
621         IF_TRUE_GOTO(pRoot == NULL, ERROR_INVALID_DATA);
622         paChilds.RemoveAll();
623         nResult = pRoot->Find(&paChilds, _T("KeyInfo"), _T("CipherValue"), NULL);
624
625         IF_TRUE_GOTO( nResult != 0, ERROR_INVALID_DATA );
626         IF_TRUE_GOTO( paChilds.GetCount() != 1, ERROR_INVALID_DATA );
627
628         pElement = (CXMLElement*)paChilds.Get(0);
629         pszValue = pElement->GetValue();
630         IF_TRUE_GOTO(pszValue == NULL, -2);
631
632         len_enc = TAPPS_STRLEN(pszValue);
633         len_dec = len_enc / 4 * 3;
634
635         DRM_TAPPS_LOG("CEK raw = [%s], len_enc = [%d], len_dec = [%d]", pszValue, len_enc, len_dec);
636
637 #if 0
638         pbBuffer = (unsigned char*)DTAPPS_MALLOC(len_dec + 1);
639         if (pbBuffer == NULL)
640         {
641                 DRM_TAPPS_EXCEPTION("Memory Allocation Error pbBuffer = %p", pbBuffer);
642                 nResult = -2;
643                 goto finish;
644         }
645
646         DTAPPS_MEMSET(pbBuffer, 0x00, len_dec + 1);
647 #endif
648
649         pbBuffer = Base64Decode(pszValue, &len_dec);
650         if (pbBuffer == NULL)
651         {
652                 DRM_TAPPS_EXCEPTION("Memory Allocation Error pbBuffer = %p", pbBuffer);
653                 nResult = -2;
654                 goto finish;
655         }
656
657         if (len_dec > (len_enc / 4 * 3))
658         {
659                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! pbBuffer=%s, len_dec=%d, (len_enc/4*3)=%d", pbBuffer, len_dec, (len_enc / 4 * 3));
660                 nResult = -1;
661                 goto finish;
662         }
663         if (len_dec != CEK_SIZE)
664         {
665                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! CEK size=%d, len_dec=%d", CEK_SIZE, len_dec);
666                 nResult = -1;
667                 goto finish;
668         }
669
670         DRM_TAPPS_LOG("Base64Decoded CEK pbBuffer=%s, len_dec=%d, (len_enc/4*3)=%d", pbBuffer, len_dec, (len_enc / 4 * 3));
671
672         t_RO->t_Content.CEK = (TADC_U8*)DTAPPS_MALLOC(CEK_SIZE + 1);
673         IF_TRUE_GOTO(t_RO->t_Content.CEK == NULL, -2);
674         DTAPPS_MEMSET(t_RO->t_Content.CEK, 0x00, CEK_SIZE + 1);
675         DTAPPS_MEMCPY(t_RO->t_Content.CEK, pbBuffer, CEK_SIZE);
676         DRM_TAPPS_SECURE_LOG("CEK t_RO->t_Content.CEK = %s", t_RO->t_Content.CEK);
677
678         // Get individual
679         pRoot = oXMLFile.GetRoot();
680         IF_TRUE_GOTO( pRoot == NULL, ERROR_INVALID_DATA );
681
682         paChilds.RemoveAll();
683         nResult = pRoot->Find(&paChilds, _T("DeviceInfo"), _T("DUID"), NULL);
684
685         IF_TRUE_GOTO(nResult != 0, ERROR_INVALID_DATA);
686         IF_TRUE_GOTO(paChilds.GetCount() != 1, ERROR_INVALID_DATA);
687
688         pElement = (CXMLElement*)paChilds.Get(0);
689         pszValue = pElement->GetValue();
690         IF_TRUE_GOTO(pszValue == NULL, -1);
691         length = TADC_IF_StrLen((char*)pszValue);
692         if (length <= 0)
693         {
694                 nResult = -1;
695                 goto finish;
696         }
697
698         t_RO->PerFlag |= DUID_RULE;
699         t_RO->t_Permission.t_Individual.BindingType |= DUID_RULE;
700         t_RO->t_Permission.t_Individual.DUID = (TADC_U8*)TADC_IF_Malloc(length + 1);
701
702         if (t_RO->t_Permission.t_Individual.DUID == NULL)
703         {
704                 DRM_TAPPS_EXCEPTION("TADC_IF_MemCmp Error Code = %x", TADC_MEMAlOC_ERROR);
705                 nResult = -2;
706                 goto finish;
707         }
708
709         TADC_IF_MemSet(t_RO->t_Permission.t_Individual.DUID, 0, length + 1);
710         TADC_IF_MemCpy((CHAR*)t_RO->t_Permission.t_Individual.DUID, pszValue, length);
711
712         nResult = 0;
713
714 finish:
715         pszValue = NULL;
716
717         if (pbBuffer)
718         {
719                 DTAPPS_FREE(pbBuffer);
720         }
721
722         if (nResult < 0)
723         {
724                 DRM_TAPPS_EXCEPTION(" Error Code = %x", TADC_XMLPARSER_ERROR);
725         }
726
727         if (nResult == -2)
728         {
729                 DRM_TAPPS_EXCEPTION(" Error Code = %x", TADC_MEMAlOC_ERROR);
730         }
731
732         return nResult;
733 }
734
735 BOOL DTappsGetCEK(const char* szCid, T_RO* t_RO)
736 {
737         int db_buf_enclen = 0, hash_buf_enclen = 0, db_buf_declen = 0, hash_buf_declen = 0;
738         DTAPPS_RIGHTS_ROW row;
739         BOOL check = FALSE;
740
741         unsigned char const_buffer_dec[512] = {0, };
742         unsigned char encr_cek[128] = {0, };
743
744         unsigned char hash_buffer[64] = {0, };
745         unsigned char md[64] = {0, };
746         unsigned int md_len = 0;
747
748         unsigned char *pDevKey = NULL;
749         unsigned int DevKeyLen = 0;
750
751         int check_valid = TADC_LICENSE_VALID;
752         DTAPPS_CONSTRAINTS st_constaints = {0, };
753
754         AES_KEY key;
755
756         memset(&key, 0x00, sizeof(key));
757
758         if (NULL == szCid || NULL == t_RO)
759         {
760                 DRM_TAPPS_EXCEPTION("Null Parameters!! szCid = %p, t_RO = %p", szCid, t_RO);
761                 goto DTAPPS_ERROR;
762         }
763
764         check = DTapps_RIGHTS_SELECT_ONE(szCid, &row);
765         if (check != TRUE)
766         {
767                 DRM_TAPPS_EXCEPTION("There is no RO in the DB. cid=%s", szCid);
768                 check_valid = TADC_LICENSE_NO_LICENSE;
769                 goto DTAPPS_ERROR;
770         }
771
772         if (0 == DTappsGetDeviceKey(&pDevKey, &DevKeyLen))
773         {
774                 DRM_TAPPS_EXCEPTION("error in DTappsGetDeviceKey pDevKey = %s, DevKeyLen = %d", pDevKey, DevKeyLen);
775                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
776                 goto DTAPPS_ERROR;
777         }
778         DRM_TAPPS_LOG("DTappsGetDeviceKey pDevKey = %s, DevKeyLen = %d", pDevKey, DevKeyLen);
779
780         check_valid = TADC_LICENSE_VALID;
781
782         /* BASE64 Decode the constraints buffer extracted from the DB */
783         db_buf_enclen = TAPPS_STRLEN(row.constraint_buffer);
784         db_buf_declen = db_buf_enclen / 4 * 3;
785         if (db_buf_enclen <= 0)
786         {
787                 DRM_TAPPS_EXCEPTION("There is no constaints buffer in the DB. cid = %s", szCid);
788                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
789                 goto DTAPPS_ERROR;
790         }
791
792         DRM_TAPPS_FRQ_LOG("db_buf_enclen = %d, db_buf_declen = %d", db_buf_enclen, db_buf_declen);
793
794         (void)DTappsB64Decode((unsigned char*)row.constraint_buffer, db_buf_enclen, const_buffer_dec, db_buf_declen);
795         if (db_buf_declen > (db_buf_enclen / 4 * 3))
796         {
797                 DRM_TAPPS_EXCEPTION("BASE64 Decode error! const_buffer_dec=%s, db_buf_declen=%d, (db_buf_enclen/4*3)=%d", const_buffer_dec, db_buf_declen, (db_buf_enclen / 4 * 3));
798                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
799                 goto DTAPPS_ERROR;
800         }
801
802         /* BASE64 Decode the hash buffer extracted from the DB */
803         DTAPPS_MEMSET(hash_buffer, 0x00, sizeof(hash_buffer));
804         DTAPPS_MEMSET(md, 0x00, sizeof(md));
805
806         hash_buf_enclen = TAPPS_STRLEN(row.constraint_hash);
807         hash_buf_declen = hash_buf_enclen / 4 * 3;
808         if (hash_buf_enclen <= 0)
809         {
810                 DRM_TAPPS_EXCEPTION("There is no hash buffer in the DB. cid = %s", szCid);
811                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
812                 goto DTAPPS_ERROR;
813         }
814         DRM_TAPPS_FRQ_LOG("hash_buf_enclen = %d, hash_buf_declen = %d", hash_buf_enclen, hash_buf_declen);
815
816         (void)DTappsB64Decode((unsigned char*)row.constraint_hash, hash_buf_enclen, hash_buffer, hash_buf_declen);
817         if (hash_buf_declen > (hash_buf_enclen / 4 * 3))
818         {
819                 DRM_TAPPS_EXCEPTION("BASE64 Decode error! hash_buffer_dec=%s, hash_buf_declen=%d, (hash_buf_enclen/4*3)=%d", hash_buffer, hash_buf_declen, (hash_buf_enclen / 4 * 3));
820                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
821                 goto DTAPPS_ERROR;
822         }
823
824         /* Calculate the hash using HMAC from the BASE64Decoded Constraint Buffers and check with the BASE64Decoded Hash buffers */
825         DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, const_buffer_dec, db_buf_declen, md, &md_len);
826         if (DTAPPS_MEMCMP(md, hash_buffer, hash_buf_declen) == 0)
827         {
828                 DRM_TAPPS_LOG("HASH for constraint buffer matching! md=%s, hash_buffer_dec=%s, hash_buf_declen=%d", md, hash_buffer, hash_buf_declen);
829         }
830         else
831         {
832                 DRM_TAPPS_EXCEPTION("HASH for constraint buffer not matching! md=%s, hash_buffer_dec=%s, hash_buf_declen=%d", md, hash_buffer, hash_buf_declen);
833                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
834                 goto DTAPPS_ERROR;
835         }
836
837         /* Validate the constraints buffer */
838         DTAPPS_MEMCPY(&st_constaints, const_buffer_dec, sizeof(DTAPPS_CONSTRAINTS));
839
840         check_valid = DTappsValidateConstraints(&st_constaints, DTAPPS_VERIFY);
841         if (check_valid != TADC_LICENSE_VALID)
842         {
843                 DRM_TAPPS_LOG("Invalid Constraints for constraints_buffer=%s, check_valid=%d", const_buffer_dec, check_valid);
844                 /* TODO: Implementation of Auto Deletion feature to delete the rights from the DB if the rights are expired!!!*/
845                 goto DTAPPS_ERROR;
846         }
847         else
848         {
849                 DRM_TAPPS_LOG("Rights are Valid! check_valid = %d", check_valid);
850
851                 /* Constraints valid - Extract CEK and fill the t_RO structure with CEK, DUID */
852                 /* BASE64 Decode the CEK buffer extracted from the DB */
853                 db_buf_enclen = TAPPS_STRLEN(row.cek);
854                 db_buf_declen = db_buf_enclen / 4 * 3;
855                 if (db_buf_enclen <= 0)
856                 {
857                         DRM_TAPPS_EXCEPTION("There is no CEK in the DB. cid = %s", szCid);
858                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
859                         goto DTAPPS_ERROR;
860                 }
861                 DRM_TAPPS_FRQ_LOG("db_buf_enclen = %d, db_buf_declen = %d", db_buf_enclen, db_buf_declen);
862
863                 int cek_declen = 0;
864
865                 (void)DTappsB64Decode((unsigned char*)row.cek, db_buf_enclen, encr_cek, cek_declen);
866                 db_buf_declen = cek_declen;
867
868                 if (cek_declen > (db_buf_enclen / 4 * 3))
869                 {
870                         DRM_TAPPS_EXCEPTION("BASE64 Decode error! encr_cek=%s, cek_declen=%d, (db_buf_enclen/4*3)=%d", encr_cek, cek_declen, (db_buf_enclen / 4 * 3));
871                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
872                         goto DTAPPS_ERROR;
873                 }
874                 DRM_TAPPS_LOG("BASE64 encr_cek=%s, cek_declen=%d, db_buf_declen=%d, (db_buf_enclen/4*3)=%d", encr_cek, cek_declen, db_buf_declen, (db_buf_enclen / 4 * 3));
875
876                 /* BASE64 Decode the hash buffer extracted from the DB */
877                 DTAPPS_MEMSET(hash_buffer, 0x00, sizeof(hash_buffer));
878                 DTAPPS_MEMSET(md, 0x00, sizeof(md));
879
880                 hash_buf_enclen = TAPPS_STRLEN(row.cek_hash);
881                 hash_buf_declen = hash_buf_enclen / 4 * 3;
882                 if (db_buf_enclen <= 0)
883                 {
884                         DRM_TAPPS_EXCEPTION("There is no CEK_hash buffer in the DB. cid = %s", szCid);
885                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
886                         goto DTAPPS_ERROR;
887                 }
888                 DRM_TAPPS_FRQ_LOG("hash_buf_enclen = %d, hash_buf_declen = %d", hash_buf_enclen, hash_buf_declen);
889
890                 (void)DTappsB64Decode((unsigned char*)row.cek_hash, hash_buf_enclen, hash_buffer, hash_buf_declen);
891                 if (hash_buf_declen > (hash_buf_enclen / 4 * 3))
892                 {
893                         DRM_TAPPS_EXCEPTION("BASE64 Decode error! hash_buffer_dec=%s, hash_buf_declen=%d, (hash_buf_enclen/4*3)=%d", hash_buffer, hash_buf_declen, (hash_buf_enclen/4*3));
894                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
895                         goto DTAPPS_ERROR;
896                 }
897
898                 /* Calculate the hash using HMAC from the BASE64Decoded Encrypted CEK Buffer and check with the BASE64Decoded Hash buffer */
899                 DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, encr_cek, db_buf_declen, md, &md_len);
900                 if (DTAPPS_MEMCMP(md, hash_buffer, hash_buf_declen) == 0)
901                 {
902                         DRM_TAPPS_LOG("HASH for Encrypted CEK buffer matching! md=%s, hash_buffer_dec=%s, hash_buf_declen=%d", md, hash_buffer, hash_buf_declen);
903                 }
904                 else
905                 {
906                         DRM_TAPPS_EXCEPTION("HASH for Encrypted CEK buffer not matching! md=%s, hash_buffer_dec=%s, hash_buf_declen=%d", md, hash_buffer, hash_buf_declen);
907                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
908                         goto DTAPPS_ERROR;
909                 }
910
911                 /* Allocate memory for CEK */
912                 t_RO->t_Content.CEK = (unsigned char*)DTAPPS_MALLOC(CEK_SIZE + 1);
913                 if (NULL == t_RO->t_Content.CEK)
914                 {
915                         DRM_TAPPS_EXCEPTION("Memory Allocation Error!! t_RO->t_Content.CEK = %p", t_RO->t_Content.CEK);
916                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
917                         goto DTAPPS_ERROR;
918                 }
919
920                 DTAPPS_MEMSET(t_RO->t_Content.CEK, 0x00, CEK_SIZE + 1);
921                 /* Decrypt CEK and update t_RO structure */
922                 if (DTAPPS_AES_SET_DECR_KEY(pDevKey, 128, &key))
923                 {
924                         DRM_TAPPS_EXCEPTION("AES_set_decrypt_key failed!!");
925                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
926                         goto DTAPPS_ERROR;
927                 }
928
929                 (void)DTAPPS_AES_UNWRAP_KEY(&key, NULL, t_RO->t_Content.CEK, encr_cek, cek_declen);
930                 DRM_TAPPS_LOG("DTAPPS_AES_UNWRAP_KEY success!!");
931
932                 /* Store DUID in t_RO */
933                 if (st_constaints.DUID[0] != '\0')
934                 {
935                         t_RO -> t_Permission.t_Individual.DUID = (unsigned char*)DTAPPS_MALLOC(DUID_SIZE + 1);
936                         if (NULL == t_RO -> t_Permission.t_Individual.DUID)
937                         {
938                                 DRM_TAPPS_EXCEPTION("Memory Allocation Error!! t_RO->t_Permission.t_Individual.DUID = %p", t_RO -> t_Permission.t_Individual.DUID);
939                                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
940                                 goto DTAPPS_ERROR;
941                         }
942                         DTAPPS_MEMSET(t_RO -> t_Permission.t_Individual.DUID, 0x00, DUID_SIZE + 1);
943                         t_RO->PerFlag |= DUID_RULE;
944                         t_RO->t_Permission.t_Individual.BindingType |= DUID_RULE;
945                         DTAPPS_MEMCPY(t_RO -> t_Permission.t_Individual.DUID, st_constaints.DUID, DUID_SIZE);
946                 }
947         }
948
949 DTAPPS_ERROR:
950         if (pDevKey)
951         {
952                 DTappsFreeDeviceKey(&pDevKey);
953         }
954
955         if (check_valid != TADC_LICENSE_VALID)
956         {
957                 DRM_TAPPS_EXCEPTION("DTappsGetCEK FAILED!!!, check_valid = %d", check_valid);
958                 return FALSE;
959         }
960         else
961         {
962                 /* All fine... Allocate memory for CID and fill the value */
963                 if ((t_RO != NULL) && (szCid != NULL))
964                 {
965                         t_RO -> t_Content.CID = (unsigned char*) DTAPPS_MALLOC(CID_SIZE + 1);
966                         if (NULL == t_RO -> t_Content.CID)
967                         {
968                                 DRM_TAPPS_EXCEPTION("Memory Allocation Error!! t_RO->t_Content.CID = %p", t_RO -> t_Content.CID);
969                                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
970                                 goto DTAPPS_ERROR;
971                         }
972                         DTAPPS_MEMSET(t_RO -> t_Content.CID, 0x00, CID_SIZE + 1);
973                         DTAPPS_MEMCPY(t_RO -> t_Content.CID, szCid, CID_SIZE);
974                         DRM_TAPPS_LOG("DTappsGetCEK SUCCESS!!!, check_valid = %d", check_valid);
975                 }
976
977                 return TRUE;
978         }
979 }
980
981 BOOL DTapps_RIGHTS_INSTALL(const char* name,
982                           const char* time,
983                           #ifdef DTAPPS_STORE_CEK_IN_DB
984                           const char* cek,
985                           const char* cek_hash,
986                           #endif
987                           const char* constraint_buffer,
988                           const char* constraint_hash,
989                           const char* cid,
990                           BOOL isUpdate)
991 {
992     void*         pDb       = NULL;
993     const char*   query     = NULL;
994     sqlite3_stmt* pstmt     = NULL;
995     unsigned int  dIdx      = 0;
996     BOOL          ret_value = FALSE;
997
998     DRM_TAPPS_LOG("Open DB......");
999     ret_value = DTappsDBOpen(pDb,__func__);
1000     if(ret_value != TRUE)
1001     {
1002         DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
1003         return FALSE;
1004     }
1005
1006     if(isUpdate == FALSE)
1007     {
1008         query = "INSERT INTO dtapps2rights (name, time_t, "
1009                                #ifdef DTAPPS_STORE_CEK_IN_DB
1010                                " cek, cek_hash, "
1011                                #endif
1012                                " constraint_buffer, constraint_hash, cid) "
1013                                " VALUES (?, ?, "
1014                                #ifdef DTAPPS_STORE_CEK_IN_DB
1015                                " ?, ?, "
1016                                #endif
1017                                " ?, ?, ?);";
1018     }
1019     else
1020     {
1021         query = "UPDATE dtapps2rights SET name=?, time_t=?,"
1022                                #ifdef DTAPPS_STORE_CEK_IN_DB
1023                                " cek=?, cek_hash=?, "
1024                                #endif
1025                                " constraint_buffer=?, constraint_hash=? "
1026                                " WHERE r_id=?;";
1027     }
1028
1029     DRM_TAPPS_LOG("Prepare Statement........");
1030     pstmt = (sqlite3_stmt*) DTappsStmtPrepare(pDb, query);
1031     if(pstmt == NULL)
1032     {
1033         DRM_TAPPS_EXCEPTION("Prepare Statement failed. query=%s", query);
1034         goto Error_Exit;
1035     }
1036
1037     DRM_TAPPS_LOG("Binding Parameters........");
1038     ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR, (void *)name, strlen(name));
1039     if(ret_value != TRUE)
1040     {
1041         DRM_TAPPS_EXCEPTION("Binding Parameter(name=%s) failed. ret_value = %d", name, ret_value);
1042         goto Error_Exit;
1043     }
1044
1045     ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR, (void *)time, strlen(time));
1046     if(ret_value != TRUE)
1047     {
1048         DRM_TAPPS_EXCEPTION("Binding Parameter(time=%s) failed. ret_value = %d", time, ret_value);
1049         goto Error_Exit;
1050     }
1051
1052     #ifdef DTAPPS_STORE_CEK_IN_DB
1053     ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR, (void *)cek, strlen(cek));
1054     if(ret_value != TRUE)
1055     {
1056         DRM_TAPPS_EXCEPTION("Binding Parameter(cek=%s) failed. ret_value = %d", cek, ret_value);
1057         goto Error_Exit;
1058     }
1059
1060     ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR, (void *)cek_hash, strlen(cek_hash));
1061     if(ret_value != TRUE)
1062     {
1063         DRM_TAPPS_EXCEPTION("Binding Parameter(cek_hash=%s) failed. ret_value = %d", cek_hash, ret_value);
1064         goto Error_Exit;
1065     }
1066     #endif
1067
1068     ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR, (void *)constraint_buffer, strlen(constraint_buffer));
1069     if(ret_value != TRUE)
1070     {
1071         DRM_TAPPS_EXCEPTION("Binding Parameter(constraint_buffer=%s) failed. ret_value = %d", constraint_buffer, ret_value);
1072         goto Error_Exit;
1073     }
1074
1075     ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR, (void *)constraint_hash, strlen(constraint_hash));
1076     if(ret_value != TRUE)
1077     {
1078         DRM_TAPPS_EXCEPTION("Binding Parameter(constraint_hash=%s) failed. ret_value = %d", constraint_hash, ret_value);
1079         goto Error_Exit;
1080     }
1081
1082     ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR, (void *)cid, strlen(cid));
1083     if(ret_value != TRUE)
1084     {
1085         DRM_TAPPS_EXCEPTION("Binding Parameter(cid=%s) failed. ret_value = %d", cid, ret_value);
1086         goto Error_Exit;
1087     }
1088
1089     DRM_TAPPS_LOG("Begin Transaction........");
1090     ret_value = DTappsDBBeginImmedTrans(__func__);
1091     if(ret_value != TRUE)
1092     {
1093         DRM_TAPPS_EXCEPTION("DB Begin Transaction ret_value = %d",ret_value);
1094         goto Error_Exit;
1095     }
1096
1097     DRM_TAPPS_LOG("Execute SQL to Insert Contents into Table........");
1098     ret_value = DTappsStmtExecute(pstmt);
1099     if(ret_value != TRUE)
1100     {
1101         DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! ret_value = %d",ret_value);
1102         goto Error_Exit;
1103     }
1104
1105     DRM_TAPPS_LOG("Commit DB........");
1106     ret_value = DTappsDBCommit(__func__);
1107     if(ret_value != TRUE)
1108     {
1109         DRM_TAPPS_EXCEPTION("Commit DB Failed!! ret_value = %d",ret_value);
1110         goto Error_Exit;
1111     }
1112
1113     DRM_TAPPS_LOG("Release Statement........");
1114     ret_value = DTappsStmtRelease(pstmt);
1115     if(ret_value != TRUE)
1116     {
1117         DRM_TAPPS_EXCEPTION("Release Statement Failed!! ret_value = %d",ret_value);
1118         pstmt = NULL;
1119         goto Error_Exit;
1120     }
1121
1122     DRM_TAPPS_LOG("Close DB........");
1123     ret_value = DTappsDBClose(__func__);
1124     if(ret_value != TRUE)
1125     {
1126         DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
1127         goto Error_Exit;
1128     }
1129
1130     DRM_TAPPS_LOG("Install DB Operartion Successful!!!");
1131     return TRUE;
1132
1133 Error_Exit:
1134     if(pstmt != NULL)
1135         DTappsStmtRelease(pstmt);
1136
1137     ret_value = DTappsDBRollback (__func__);
1138     if(ret_value != TRUE)
1139     {
1140         DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d",ret_value);
1141     }
1142
1143     ret_value = DTappsDBClose(__func__);
1144     if(ret_value != TRUE)
1145     {
1146         DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
1147     }
1148
1149     return FALSE;
1150 }
1151
1152 BOOL DTapps_RIGHTS_SELECT_ONE(const char* cid, DTAPPS_RIGHTS_ROW* row)
1153 {
1154     void*         pDb       = NULL;
1155     const char*   query     = NULL;
1156     sqlite3_stmt* pstmt     = NULL;
1157     unsigned int  dIdx      = 0;
1158     BOOL          ret_value = FALSE;
1159     int           db_ret    = -1;
1160     DTAPPS_RIGHTS_ROW* result = row;
1161
1162     char *name, *time, *constraint_buffer, *constraint_hash;
1163     #ifdef DTAPPS_STORE_CEK_IN_DB
1164     char *cek, *cek_hash;
1165     #endif
1166
1167     DRM_TAPPS_LOG("Open DB......");
1168     ret_value = DTappsDBOpen(pDb,__func__);
1169     if(ret_value != TRUE)
1170     {
1171         DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d",ret_value);
1172         return FALSE;
1173     }
1174
1175     query = "SELECT r_id, name, time_t, "
1176             #ifdef DTAPPS_STORE_CEK_IN_DB
1177             " cek, cek_hash, "
1178             #endif
1179             " constraint_buffer, constraint_hash "
1180             " FROM dtapps2rights"
1181             " WHERE cid=? ORDER BY r_id desc limit 1;";
1182
1183
1184     DRM_TAPPS_LOG("Begin Transaction........");
1185     ret_value = DTappsDBBeginImmedTrans(__func__);
1186     if(ret_value != TRUE)
1187     {
1188         DRM_TAPPS_EXCEPTION("DB Begin Transaction Failed!! ret_value = %d",ret_value);
1189         goto Error_Exit;
1190     }
1191
1192     DRM_TAPPS_LOG("Prepare Statement........");
1193     db_ret = sqlite3_prepare((sqlite3*)pDb, query, -1, &pstmt, NULL);
1194     if(pstmt == NULL)
1195     {
1196         DRM_TAPPS_EXCEPTION("Prepare Statement failed. query=%s, error=%s", query, sqlite3_errmsg((sqlite3*)pDb));
1197         goto Error_Exit;
1198     }
1199
1200     DRM_TAPPS_LOG("Binding Parameters........");
1201     db_ret = sqlite3_bind_text(pstmt, ++dIdx, cid, strlen(cid), SQLITE_TRANSIENT);
1202     if(db_ret != SQLITE_OK)
1203     {
1204         DRM_TAPPS_EXCEPTION("Binding Parameter(cid=%s) failed. ret_value = %d, query=%s", cid, db_ret, query);
1205         goto Error_Exit;
1206     }
1207
1208     DRM_TAPPS_LOG("Execute SQL to Select Contents from Table........");
1209     db_ret = sqlite3_step(pstmt);
1210     if(db_ret == SQLITE_OK || db_ret == SQLITE_DONE)
1211     {
1212         DRM_TAPPS_FRQ_LOG("Executed SQL Select Query. But no row was returned");
1213         goto Error_Exit;
1214     }
1215     else if(db_ret != SQLITE_ROW)
1216     {
1217         DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! ret_value = %d, query=%s, error=%s",
1218                             db_ret, query, sqlite3_errmsg((sqlite3*)pDb));
1219         goto Error_Exit;
1220     }
1221
1222     memset(result, 0, sizeof(DTAPPS_RIGHTS_ROW));
1223
1224     dIdx = 0;
1225     result->r_id = sqlite3_column_int(pstmt, dIdx++);
1226     DRM_TAPPS_LOG("....SEELECTED : r_id = %d", result->r_id);
1227
1228     name = (char*) sqlite3_column_text(pstmt, dIdx++);
1229     if(name != NULL)
1230         memcpy(result->name, name, strlen(name));
1231     DRM_TAPPS_LOG("....SEELECTED : name = %s", result->name);
1232
1233     time= (char*) sqlite3_column_text(pstmt, dIdx++);
1234     if(time != NULL)
1235         memcpy(result->time, time, strlen(time));
1236     DRM_TAPPS_LOG("....SEELECTED : time = %s", result->time);
1237
1238     #ifdef DTAPPS_STORE_CEK_IN_DB
1239     cek= (char*) sqlite3_column_text(pstmt, dIdx++);
1240     if(cek != NULL)
1241         memcpy(result->cek, cek, strlen(cek));
1242     DRM_TAPPS_LOG("....SEELECTED : cek = %s", result->cek);
1243
1244     cek_hash= (char*) sqlite3_column_text(pstmt, dIdx++);
1245     if(cek_hash != NULL)
1246         memcpy(result->cek_hash, cek_hash, strlen(cek_hash));
1247     DRM_TAPPS_LOG("....SEELECTED : cek_hash = %s", result->cek_hash);
1248     #endif
1249
1250     constraint_buffer = (char*) sqlite3_column_text(pstmt, dIdx++);
1251     if(constraint_buffer != NULL)
1252         memcpy(result->constraint_buffer, constraint_buffer, strlen(constraint_buffer));
1253     DRM_TAPPS_LOG("....SEELECTED : constraint_buffer = %s", result->constraint_buffer);
1254
1255     constraint_hash = (char*) sqlite3_column_text(pstmt, dIdx++);
1256     if(constraint_hash != NULL)
1257         memcpy(result->constraint_hash, constraint_hash, strlen(constraint_hash));
1258     DRM_TAPPS_LOG("....SEELECTED : constraint_hash = %s", result->constraint_hash);
1259
1260     memcpy(result->cid, cid, strlen(cid));
1261     DRM_TAPPS_LOG("....SEELECTED : cid = %s", result->cid);
1262
1263     DRM_TAPPS_LOG("Release Statement........");
1264     ret_value = DTappsStmtRelease(pstmt);
1265     if(ret_value != TRUE)
1266     {
1267         DRM_TAPPS_EXCEPTION("Release Statement Failed!! ret_value = %d",ret_value);
1268         pstmt = NULL;
1269         goto Error_Exit;
1270     }
1271
1272     DRM_TAPPS_LOG("Close DB........");
1273     ret_value = DTappsDBClose(__func__);
1274     if(ret_value != TRUE)
1275     {
1276         DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
1277         goto Error_Exit;
1278     }
1279
1280     DRM_TAPPS_LOG("Select a DB row Operartion Successful!!!");
1281     return TRUE;
1282
1283 Error_Exit:
1284     if(pstmt)
1285         DTappsStmtRelease(pstmt);
1286
1287     ret_value = DTappsDBClose(__func__);
1288     if(ret_value != TRUE)
1289     {
1290         DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d",ret_value);
1291     }
1292
1293     return FALSE;
1294 }