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