55403e059d42aef188d1e7ed4b5f0e112832d767
[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) + 1);
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         if (pDevKey)
371                 DTappsFreeDeviceKey(&pDevKey);
372
373         return ret;
374 }
375
376 int DTappsHasValidLicense(const char *szCid)
377 {
378         int const_buf_enclen = 0;
379         int hash_buf_enclen = 0;
380         int const_buf_declen = 0;
381         int hash_buf_declen = 0;
382
383         unsigned char *pDevKey = NULL;
384         unsigned int DevKeyLen = 0;
385         unsigned char const_buffer_dec[512] = {0, };
386         unsigned char hash_buffer_dec[64] = {0, };
387         unsigned char md[64] = {0, };
388         unsigned int md_len = 0;
389
390         DTAPPS_RIGHTS_ROW row;
391         bool check = FALSE;
392
393         int check_valid = TADC_LICENSE_VALID;
394         DTAPPS_CONSTRAINTS st_constaints = {0, };
395
396         check = DTapps_RIGHTS_SELECT_ONE(szCid, &row);
397
398         if (check != TRUE) {
399                 DRM_TAPPS_EXCEPTION("There is no RO in the DB. cid  = %s", szCid);
400                 check_valid = TADC_LICENSE_NO_LICENSE;
401                 goto DTAPPS_END;
402         }
403
404         if (0 == DTappsGetDeviceKey(&pDevKey, &DevKeyLen)) {
405                 DRM_TAPPS_EXCEPTION("Error in DTAppsGetDeviceKey pDevKey = %s, DevKeyLen = %d",
406                                                         pDevKey, DevKeyLen);
407                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
408                 goto DTAPPS_END;
409         }
410
411         /* BASE64 Decode the constraints buffer extracted from the DB */
412         const_buf_enclen = TAPPS_STRLEN(row.constraint_buffer);
413         const_buf_declen = const_buf_enclen / 4 * 3;
414
415         if (const_buf_enclen <= 0) {
416                 DRM_TAPPS_EXCEPTION("There is no constaints buffer in the DB. cid = %s", szCid);
417                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
418                 goto DTAPPS_END;
419         }
420
421         DRM_TAPPS_FRQ_LOG("const_buf_enclen = %d, const_buf_declen = %d",
422                                           const_buf_enclen, const_buf_declen);
423
424         (void)DTappsB64Decode((unsigned char *)row.constraint_buffer, const_buf_enclen,
425                                                   const_buffer_dec, const_buf_declen);
426
427         if (const_buf_declen > (const_buf_enclen / 4 * 3)) {
428                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! const_buffer_dec = %s, "
429                                                         "const_buf_declen = %d, (const_buf_enclen/4*3) = %d",
430                                                         const_buffer_dec, const_buf_declen,
431                                                         (const_buf_enclen / 4 * 3));
432                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
433                 goto DTAPPS_END;
434         }
435
436         /* BASE64 Decode the hash buffer extracted from the DB */
437         DTAPPS_MEMSET(hash_buffer_dec, 0x00, sizeof(hash_buffer_dec));
438         DTAPPS_MEMSET(md, 0x00, sizeof(md));
439
440         hash_buf_enclen = TAPPS_STRLEN(row.constraint_hash);
441         hash_buf_declen = hash_buf_enclen / 4 * 3;
442
443         if (hash_buf_enclen <= 0) {
444                 DRM_TAPPS_EXCEPTION("There is no constaints_hash buffer in the DB. cid = %s",
445                                                         szCid);
446                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
447                 goto DTAPPS_END;
448         }
449
450         DRM_TAPPS_FRQ_LOG("hash_buf_enclen = %d, hash_buf_declen = %d", hash_buf_enclen,
451                                           hash_buf_declen);
452
453         (void)DTappsB64Decode((unsigned char *)row.constraint_hash, hash_buf_enclen,
454                                                   hash_buffer_dec, hash_buf_declen);
455
456         if (hash_buf_declen > (hash_buf_enclen / 4 * 3)) {
457                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! hash_buffer_dec = %s, "
458                                                         "hash_buf_declen = %d, (hash_buf_enclen/4*3) = %d",
459                                                         hash_buffer_dec, hash_buf_declen,
460                                                         (hash_buf_enclen / 4 * 3));
461                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
462                 goto DTAPPS_END;
463         }
464
465         // Calculate the hash using HMAC from the BASE64Decoded Constraint Buffers
466         // and check with the BASE64Decoded Hash buffers
467         DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, const_buffer_dec, const_buf_declen,
468                                           md, &md_len);
469
470         if (DTAPPS_MEMCMP(md, hash_buffer_dec, hash_buf_declen) == 0) {
471                 DRM_TAPPS_LOG("HASH matching!!! md = %s, hash_buffer_dec = %s, "
472                                           "hash_buf_declen = %d",
473                                           md, hash_buffer_dec, hash_buf_declen);
474         } else {
475                 DRM_TAPPS_EXCEPTION("HASH not matching!!! md = %s, hash_buffer_dec = %s, "
476                                                         "hash_buf_declen = %d",
477                                                         md, hash_buffer_dec, hash_buf_declen);
478                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
479                 goto DTAPPS_END;
480         }
481
482         /* Validate the constraints buffer */
483         DTAPPS_MEMCPY(&st_constaints, const_buffer_dec, sizeof(DTAPPS_CONSTRAINTS));
484
485         check_valid = DTappsValidateConstraints(&st_constaints, DTAPPS_VERIFY);
486
487         if (check_valid != TADC_LICENSE_VALID) {
488                 DRM_TAPPS_LOG("Invalid Constraints for constraints_buffer = %s "
489                                           "check_valid = %d",
490                                           const_buffer_dec, check_valid);
491                 // TODO: Implementation of Auto Deletion feature to delete the rights
492                 //       from the DB if the rights are expired!!!
493                 goto DTAPPS_END;
494         } else {
495                 DRM_TAPPS_LOG("Rights are Valid!!! Found atleast one Valid Rights..... "
496                                           "return with Success!! check_valid = %d",
497                                           check_valid);
498         }
499
500 DTAPPS_END:
501
502         if (pDevKey) {
503                 DTappsFreeDeviceKey(&pDevKey);
504         }
505
506         if (check_valid != TADC_LICENSE_VALID) {
507                 DRM_TAPPS_EXCEPTION("Constraints not Valid!!!! check_valid = %d", check_valid);
508                 DRM_TAPPS_EXCEPTION("DrmTdcHasValidLicense Failed!!!");
509                 return check_valid;
510         } else {
511                 DRM_TAPPS_LOG("Constraints are Valid!! check_valid = %d", check_valid);
512                 return TADC_LICENSE_VALID;
513         }
514 }
515
516 int DTappsGetROInfo(const char *pszXML, T_RO *t_RO, unsigned char *name)
517 {
518         int             nResult = 0;
519         CXMLFile        oXMLFile;
520         CXMLElement    *pRoot, *pElement;
521         CPointerArray   paChilds;
522         LPCTSTR         pszValue;
523         int             length = 0;
524         unsigned char   *pbBuffer = NULL;
525         int len_enc = 0, len_dec = 0;
526
527         const char *pRoHeader = "<?xml version=\"1.0\"?>\n<TizenLicense>";
528
529         //Check Param Buffer
530         if (pszXML == NULL || t_RO == NULL || name == NULL) {
531                 DRM_TAPPS_EXCEPTION("Parameters NULL!!! pszXML = %p, t_RO = %p", pszXML, t_RO);
532                 return -1;
533         }
534
535         if (memcmp(pszXML, pRoHeader, strlen(pRoHeader))) {
536                 DRM_TAPPS_EXCEPTION("TADC_IF_MemCmp. pRoHeader[%s] Error Code = %x", pRoHeader,
537                                                         TADC_XMLPARSER_ERROR);
538                 return -1;
539         }
540
541         DTAPPS_MEMSET(t_RO, 0, sizeof(T_RO));
542
543         nResult = oXMLFile.LoadFromStream((LPCTSTR)pszXML);
544
545         if (nResult < 0) {
546                 DRM_TAPPS_EXCEPTION("oXMLFile.LoadFromStream is failed\n");
547                 nResult = -1;
548                 goto finish;
549         }
550
551         // Set Version
552         pRoot = oXMLFile.GetRoot();
553
554         if (pRoot == NULL) {
555                 DRM_TAPPS_EXCEPTION("oXMLFile.GetRoot is failed\n");
556                 nResult = -1;
557                 goto finish;
558         }
559
560         // Get UID -> Name
561         pRoot = oXMLFile.GetRoot();
562
563         if (pRoot == NULL) {
564                 DRM_TAPPS_EXCEPTION("oXMLFile.GetRoot is failed\n");
565                 nResult = -1;
566                 goto finish;
567         }
568
569         paChilds.RemoveAll();
570         nResult = pRoot->Find(&paChilds, _T("LicenseInfo"), _T("uid"), NULL);
571
572         if (nResult != 0) {
573                 DRM_TAPPS_EXCEPTION("pRoot->Find is failed\n");
574                 nResult = -1;
575                 goto finish;
576         }
577
578         if (paChilds.GetCount() != 1) {
579                 DRM_TAPPS_EXCEPTION("paChilds.GetCount() is failed\n");
580                 nResult = -1;
581                 goto finish;
582         }
583
584         pElement = (CXMLElement *) paChilds.Get(0);
585         pszValue = pElement->GetValue();
586
587         if (pszValue == NULL) {
588                 DRM_TAPPS_EXCEPTION("pElement->GetValue() is failed\n");
589                 nResult = -2;
590                 goto finish;
591         }
592
593         length = TAPPS_STRLEN(pszValue);
594
595         if (length <= 0 || length > DTAPPS_NAME_SIZE) {
596                 DRM_TAPPS_EXCEPTION("TAPPS_STRLEN failed(%d) -  %s\n", length, pszValue);
597                 nResult = -1;
598                 goto finish;
599         }
600
601         memcpy(name, pszValue, length + 1);
602         DRM_TAPPS_LOG("name = %s", name);
603
604         // Get UID -> CID
605         pRoot = oXMLFile.GetRoot();
606
607         if (pRoot == NULL) {
608                 DRM_TAPPS_EXCEPTION("oXMLFile.GetRoot is failed\n");
609                 nResult = -1;
610                 goto finish;
611         }
612
613         paChilds.RemoveAll();
614         nResult = pRoot->Find(&paChilds, _T("ContentInfo"), _T("cid"), NULL);
615
616         if (nResult != 0) {
617                 DRM_TAPPS_EXCEPTION("pRoot->Find is failed\n");
618                 nResult = -1;
619                 goto finish;
620         }
621
622         if (paChilds.GetCount() != 1) {
623                 DRM_TAPPS_EXCEPTION("paChilds.GetCount() is failed\n");
624                 nResult = -1;
625                 goto finish;
626         }
627
628         pElement = (CXMLElement *)paChilds.Get(0);
629         pszValue = pElement->GetValue();
630
631         if (pszValue == NULL) {
632                 DRM_TAPPS_EXCEPTION("pElement->GetValue() is failed\n");
633                 nResult = -2;
634                 goto finish;
635         }
636
637         length = TAPPS_STRLEN(pszValue);
638
639         if (length <= 0 || length > CID_SIZE) {
640                 DRM_TAPPS_EXCEPTION("TAPPS_STRLEN is failed(%d) -  %s\n", length, pszValue);
641                 nResult = -1;
642                 goto finish;
643         }
644
645         t_RO->t_Content.CID = (TADC_U8 *)DTAPPS_MALLOC(CID_SIZE + 1);
646         IF_TRUE_GOTO(t_RO->t_Content.CID == NULL, -2);
647
648         memset(t_RO->t_Content.CID, 0, CID_SIZE + 1);
649         memcpy(t_RO->t_Content.CID, pszValue, CID_SIZE + 1);
650
651         // Get KeyValue (CEK)
652         pRoot = oXMLFile.GetRoot();
653         IF_TRUE_GOTO(pRoot == NULL, ERROR_INVALID_DATA);
654         paChilds.RemoveAll();
655         nResult = pRoot->Find(&paChilds, _T("KeyInfo"), _T("CipherValue"), NULL);
656
657         IF_TRUE_GOTO(nResult != 0, ERROR_INVALID_DATA);
658         IF_TRUE_GOTO(paChilds.GetCount() != 1, ERROR_INVALID_DATA);
659
660         pElement = (CXMLElement *)paChilds.Get(0);
661         pszValue = pElement->GetValue();
662         IF_TRUE_GOTO(pszValue == NULL, -2);
663
664         len_enc = TAPPS_STRLEN(pszValue);
665         len_dec = len_enc / 4 * 3;
666
667         DRM_TAPPS_LOG("CEK raw = [%s], len_enc = [%d], len_dec = [%d]", pszValue,
668                                   len_enc, len_dec);
669
670 #if 0
671         pbBuffer = (unsigned char *)DTAPPS_MALLOC(len_dec + 1);
672
673         if (pbBuffer == NULL) {
674                 DRM_TAPPS_EXCEPTION("Memory Allocation Error pbBuffer = %p", pbBuffer);
675                 nResult = -2;
676                 goto finish;
677         }
678
679         DTAPPS_MEMSET(pbBuffer, 0x00, len_dec + 1);
680 #endif
681
682         pbBuffer = Base64Decode(pszValue, &len_dec);
683
684         if (pbBuffer == NULL) {
685                 DRM_TAPPS_EXCEPTION("Memory Allocation Error pbBuffer = %p", pbBuffer);
686                 nResult = -2;
687                 goto finish;
688         }
689
690         if (len_dec > (len_enc / 4 * 3)) {
691                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! pbBuffer=%s, len_dec=%d, "
692                                                         "(len_enc/4*3)=%d",
693                                                         pbBuffer, len_dec, (len_enc / 4 * 3));
694                 nResult = -1;
695                 goto finish;
696         }
697
698         if (len_dec != CEK_SIZE) {
699                 DRM_TAPPS_EXCEPTION("BASE64 Decode error !!! CEK size=%d, len_dec=%d", CEK_SIZE,
700                                                         len_dec);
701                 nResult = -1;
702                 goto finish;
703         }
704
705         DRM_TAPPS_LOG("Base64Decoded CEK pbBuffer=%s, len_dec=%d, (len_enc/4*3)=%d",
706                                   pbBuffer, len_dec, (len_enc / 4 * 3));
707
708         t_RO->t_Content.CEK = (TADC_U8 *)DTAPPS_MALLOC(CEK_SIZE + 1);
709         IF_TRUE_GOTO(t_RO->t_Content.CEK == NULL, -2);
710         DTAPPS_MEMSET(t_RO->t_Content.CEK, 0x00, CEK_SIZE + 1);
711         DTAPPS_MEMCPY(t_RO->t_Content.CEK, pbBuffer, CEK_SIZE);
712         DRM_TAPPS_SECURE_LOG("CEK t_RO->t_Content.CEK = %s", t_RO->t_Content.CEK);
713
714         // Get individual
715         pRoot = oXMLFile.GetRoot();
716         IF_TRUE_GOTO(pRoot == NULL, ERROR_INVALID_DATA);
717
718         paChilds.RemoveAll();
719         nResult = pRoot->Find(&paChilds, _T("DeviceInfo"), _T("DUID"), NULL);
720
721         IF_TRUE_GOTO(nResult != 0, ERROR_INVALID_DATA);
722         IF_TRUE_GOTO(paChilds.GetCount() != 1, ERROR_INVALID_DATA);
723
724         pElement = (CXMLElement *)paChilds.Get(0);
725         pszValue = pElement->GetValue();
726         IF_TRUE_GOTO(pszValue == NULL, -1);
727         length = TADC_IF_StrLen((char *)pszValue);
728
729         if (length <= 0) {
730                 nResult = -1;
731                 goto finish;
732         }
733
734         t_RO->PerFlag |= DUID_RULE;
735         t_RO->t_Permission.t_Individual.BindingType |= DUID_RULE;
736         t_RO->t_Permission.t_Individual.DUID = (TADC_U8 *)TADC_IF_Malloc(length + 1);
737
738         if (t_RO->t_Permission.t_Individual.DUID == NULL) {
739                 DRM_TAPPS_EXCEPTION("TADC_IF_MemCmp Error Code = %x", TADC_MEMAlOC_ERROR);
740                 nResult = -2;
741                 goto finish;
742         }
743
744         TADC_IF_MemSet(t_RO->t_Permission.t_Individual.DUID, 0, length + 1);
745         TADC_IF_MemCpy((CHAR *)t_RO->t_Permission.t_Individual.DUID, pszValue, length + 1);
746
747         nResult = 0;
748
749 finish:
750         pszValue = NULL;
751
752         if (pbBuffer) {
753                 DTAPPS_FREE(pbBuffer);
754         }
755
756         if (nResult < 0) {
757                 DRM_TAPPS_EXCEPTION(" Error Code = %x", TADC_XMLPARSER_ERROR);
758         }
759
760         if (nResult == -2) {
761                 DRM_TAPPS_EXCEPTION(" Error Code = %x", TADC_MEMAlOC_ERROR);
762         }
763
764         return nResult;
765 }
766
767 BOOL DTappsGetCEK(const char *szCid, T_RO *t_RO)
768 {
769         int db_buf_enclen = 0;
770         int hash_buf_enclen = 0;
771         int db_buf_declen = 0;
772         int hash_buf_declen = 0;
773         DTAPPS_RIGHTS_ROW row;
774         BOOL check = FALSE;
775
776         unsigned char const_buffer_dec[512] = {0, };
777         unsigned char encr_cek[128] = {0, };
778
779         unsigned char hash_buffer[64] = {0, };
780         unsigned char md[64] = {0, };
781         unsigned int md_len = 0;
782
783         unsigned char *pDevKey = NULL;
784         unsigned int DevKeyLen = 0;
785
786         int check_valid = TADC_LICENSE_VALID;
787         DTAPPS_CONSTRAINTS st_constaints = {0, };
788
789         AES_KEY key;
790
791         memset(&key, 0x00, sizeof(key));
792
793         if (NULL == szCid || NULL == t_RO) {
794                 DRM_TAPPS_EXCEPTION("Null Parameters!! szCid = %p, t_RO = %p", szCid, t_RO);
795                 goto DTAPPS_ERROR;
796         }
797
798         check = DTapps_RIGHTS_SELECT_ONE(szCid, &row);
799
800         if (check != TRUE) {
801                 DRM_TAPPS_EXCEPTION("There is no RO in the DB. cid=%s", szCid);
802                 check_valid = TADC_LICENSE_NO_LICENSE;
803                 goto DTAPPS_ERROR;
804         }
805
806         if (0 == DTappsGetDeviceKey(&pDevKey, &DevKeyLen)) {
807                 DRM_TAPPS_EXCEPTION("error in DTappsGetDeviceKey pDevKey = %s, DevKeyLen = %d",
808                                                         pDevKey, DevKeyLen);
809                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
810                 goto DTAPPS_ERROR;
811         }
812
813         DRM_TAPPS_LOG("DTappsGetDeviceKey pDevKey = %s, DevKeyLen = %d", pDevKey,
814                                   DevKeyLen);
815
816         check_valid = TADC_LICENSE_VALID;
817
818         /* BASE64 Decode the constraints buffer extracted from the DB */
819         db_buf_enclen = TAPPS_STRLEN(row.constraint_buffer);
820         db_buf_declen = db_buf_enclen / 4 * 3;
821
822         if (db_buf_enclen <= 0) {
823                 DRM_TAPPS_EXCEPTION("There is no constaints buffer in the DB. cid = %s", szCid);
824                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
825                 goto DTAPPS_ERROR;
826         }
827
828         DRM_TAPPS_FRQ_LOG("db_buf_enclen = %d, db_buf_declen = %d", db_buf_enclen,
829                                           db_buf_declen);
830
831         (void)DTappsB64Decode((unsigned char *)row.constraint_buffer, db_buf_enclen,
832                                                   const_buffer_dec, db_buf_declen);
833
834         if (db_buf_declen > (db_buf_enclen / 4 * 3)) {
835                 DRM_TAPPS_EXCEPTION("BASE64 Decode error! const_buffer_dec=%s, "
836                                                         "db_buf_declen=%d, (db_buf_enclen/4*3)=%d",
837                                                         const_buffer_dec, db_buf_declen, (db_buf_enclen / 4 * 3));
838                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
839                 goto DTAPPS_ERROR;
840         }
841
842         /* BASE64 Decode the hash buffer extracted from the DB */
843         DTAPPS_MEMSET(hash_buffer, 0x00, sizeof(hash_buffer));
844         DTAPPS_MEMSET(md, 0x00, sizeof(md));
845
846         hash_buf_enclen = TAPPS_STRLEN(row.constraint_hash);
847         hash_buf_declen = hash_buf_enclen / 4 * 3;
848
849         if (hash_buf_enclen <= 0) {
850                 DRM_TAPPS_EXCEPTION("There is no hash buffer in the DB. cid = %s", szCid);
851                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
852                 goto DTAPPS_ERROR;
853         }
854
855         DRM_TAPPS_FRQ_LOG("hash_buf_enclen = %d, hash_buf_declen = %d", hash_buf_enclen,
856                                           hash_buf_declen);
857
858         (void)DTappsB64Decode((unsigned char *)row.constraint_hash, hash_buf_enclen,
859                                                   hash_buffer, hash_buf_declen);
860
861         if (hash_buf_declen > (hash_buf_enclen / 4 * 3)) {
862                 DRM_TAPPS_EXCEPTION("BASE64 Decode error! hash_buffer_dec=%s, "
863                                                         "hash_buf_declen=%d, (hash_buf_enclen/4*3)=%d",
864                                                         hash_buffer, hash_buf_declen, (hash_buf_enclen / 4 * 3));
865                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
866                 goto DTAPPS_ERROR;
867         }
868
869         // Calculate the hash using HMAC from the BASE64Decoded Constraint Buffers
870         // and check with the BASE64Decoded Hash buffers
871         DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, const_buffer_dec, db_buf_declen, md,
872                                           &md_len);
873
874         if (DTAPPS_MEMCMP(md, hash_buffer, hash_buf_declen) == 0) {
875                 DRM_TAPPS_LOG("HASH for constraint buffer matching! md=%s, "
876                                           "hash_buffer_dec=%s, hash_buf_declen=%d",
877                                           md, hash_buffer, hash_buf_declen);
878         } else {
879                 DRM_TAPPS_EXCEPTION("HASH for constraint buffer not matching! md=%s, "
880                                                         "hash_buffer_dec=%s, hash_buf_declen=%d",
881                                                         md, hash_buffer, hash_buf_declen);
882                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
883                 goto DTAPPS_ERROR;
884         }
885
886         /* Validate the constraints buffer */
887         DTAPPS_MEMCPY(&st_constaints, const_buffer_dec, sizeof(DTAPPS_CONSTRAINTS));
888
889         check_valid = DTappsValidateConstraints(&st_constaints, DTAPPS_VERIFY);
890
891         if (check_valid != TADC_LICENSE_VALID) {
892                 DRM_TAPPS_LOG("Invalid Constraints for constraints_buffer=%s, check_valid=%d",
893                                           const_buffer_dec, check_valid);
894                 // TODO: Implementation of Auto Deletion feature to delete the rights
895                 //       from the DB if the rights are expired!!!
896                 goto DTAPPS_ERROR;
897         } else {
898                 DRM_TAPPS_LOG("Rights are Valid! check_valid = %d", check_valid);
899
900                 // Constraints valid - Extract CEK and fill the t_RO structure with CEK, DUID
901                 // BASE64 Decode the CEK buffer extracted from the DB
902                 db_buf_enclen = TAPPS_STRLEN(row.cek);
903                 db_buf_declen = db_buf_enclen / 4 * 3;
904
905                 if (db_buf_enclen <= 0) {
906                         DRM_TAPPS_EXCEPTION("There is no CEK in the DB. cid = %s", szCid);
907                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
908                         goto DTAPPS_ERROR;
909                 }
910
911                 DRM_TAPPS_FRQ_LOG("db_buf_enclen = %d, db_buf_declen = %d", db_buf_enclen,
912                                                   db_buf_declen);
913
914                 int cek_declen = 0;
915
916                 (void)DTappsB64Decode((unsigned char *)row.cek, db_buf_enclen, encr_cek,
917                                                           cek_declen);
918                 db_buf_declen = cek_declen;
919
920                 if (cek_declen > (db_buf_enclen / 4 * 3)) {
921                         DRM_TAPPS_EXCEPTION("BASE64 Decode error! encr_cek=%s, cek_declen=%d, "
922                                                                 "(db_buf_enclen/4*3)=%d",
923                                                                 encr_cek, cek_declen, (db_buf_enclen / 4 * 3));
924                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
925                         goto DTAPPS_ERROR;
926                 }
927
928                 DRM_TAPPS_LOG("BASE64 encr_cek=%s, cek_declen=%d, db_buf_declen=%d, "
929                                           "(db_buf_enclen/4*3)=%d",
930                                           encr_cek, cek_declen, db_buf_declen, (db_buf_enclen / 4 * 3));
931
932                 /* BASE64 Decode the hash buffer extracted from the DB */
933                 DTAPPS_MEMSET(hash_buffer, 0x00, sizeof(hash_buffer));
934                 DTAPPS_MEMSET(md, 0x00, sizeof(md));
935
936                 hash_buf_enclen = TAPPS_STRLEN(row.cek_hash);
937                 hash_buf_declen = hash_buf_enclen / 4 * 3;
938
939                 if (hash_buf_enclen <= 0) {
940                         DRM_TAPPS_EXCEPTION("There is no CEK_hash buffer in the DB. cid = %s",
941                                                                 szCid);
942                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
943                         goto DTAPPS_ERROR;
944                 }
945
946                 DRM_TAPPS_FRQ_LOG("hash_buf_enclen = %d, hash_buf_declen = %d", hash_buf_enclen,
947                                                   hash_buf_declen);
948
949                 (void)DTappsB64Decode((unsigned char *)row.cek_hash, hash_buf_enclen,
950                                                           hash_buffer, hash_buf_declen);
951
952                 if (hash_buf_declen > (hash_buf_enclen / 4 * 3)) {
953                         DRM_TAPPS_EXCEPTION("BASE64 Decode error! hash_buffer_dec=%s, "
954                                                                 "hash_buf_declen=%d, (hash_buf_enclen/4*3)=%d",
955                                                                 hash_buffer, hash_buf_declen,
956                                                                 (hash_buf_enclen / 4 * 3));
957                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
958                         goto DTAPPS_ERROR;
959                 }
960
961                 // Calculate the hash using HMAC from the BASE64Decoded Encrypted CEK Buffer
962                 // and check with the BASE64Decoded Hash buffer
963                 DTappsCalHMACSHA1(pDevKey, (int)DevKeyLen, encr_cek, db_buf_declen, md,
964                                                   &md_len);
965
966                 if (DTAPPS_MEMCMP(md, hash_buffer, hash_buf_declen) == 0) {
967                         DRM_TAPPS_LOG("HASH for Encrypted CEK buffer matching! md=%s, "
968                                                   "hash_buffer_dec=%s, hash_buf_declen=%d",
969                                                   md, hash_buffer, hash_buf_declen);
970                 } else {
971                         DRM_TAPPS_EXCEPTION("HASH for Encrypted CEK buffer not matching! "
972                                                                 "md=%s, hash_buffer_dec=%s, hash_buf_declen=%d",
973                                                                 md, hash_buffer, hash_buf_declen);
974                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
975                         goto DTAPPS_ERROR;
976                 }
977
978                 /* Allocate memory for CEK */
979                 t_RO->t_Content.CEK = (unsigned char *)DTAPPS_MALLOC(CEK_SIZE + 1);
980
981                 if (NULL == t_RO->t_Content.CEK) {
982                         DRM_TAPPS_EXCEPTION("Memory Allocation Error!! t_RO->t_Content.CEK = %p",
983                                                                 t_RO->t_Content.CEK);
984                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
985                         goto DTAPPS_ERROR;
986                 }
987
988                 DTAPPS_MEMSET(t_RO->t_Content.CEK, 0x00, CEK_SIZE + 1);
989
990                 /* Decrypt CEK and update t_RO structure */
991                 if (DTAPPS_AES_SET_DECR_KEY(pDevKey, 128, &key)) {
992                         DRM_TAPPS_EXCEPTION("AES_set_decrypt_key failed!!");
993                         check_valid = TADC_LICENSE_UNKNOWN_ERROR;
994                         goto DTAPPS_ERROR;
995                 }
996
997                 (void)DTAPPS_AES_UNWRAP_KEY(&key, NULL, t_RO->t_Content.CEK, encr_cek,
998                                                                         cek_declen);
999                 DRM_TAPPS_LOG("DTAPPS_AES_UNWRAP_KEY success!!");
1000
1001                 /* Store DUID in t_RO */
1002                 if (st_constaints.DUID[0] != '\0') {
1003                         t_RO -> t_Permission.t_Individual.DUID = (unsigned char *)DTAPPS_MALLOC(
1004                                                 DUID_SIZE + 1);
1005
1006                         if (NULL == t_RO -> t_Permission.t_Individual.DUID) {
1007                                 DRM_TAPPS_EXCEPTION("Memory Allocation Error!! "
1008                                                                         "t_RO->t_Permission.t_Individual.DUID = %p",
1009                                                                         t_RO -> t_Permission.t_Individual.DUID);
1010                                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
1011                                 goto DTAPPS_ERROR;
1012                         }
1013
1014                         DTAPPS_MEMSET(t_RO -> t_Permission.t_Individual.DUID, 0x00, DUID_SIZE + 1);
1015                         t_RO->PerFlag |= DUID_RULE;
1016                         t_RO->t_Permission.t_Individual.BindingType |= DUID_RULE;
1017                         DTAPPS_MEMCPY(t_RO -> t_Permission.t_Individual.DUID, st_constaints.DUID,
1018                                                   DUID_SIZE);
1019                 }
1020         }
1021
1022 DTAPPS_ERROR:
1023
1024         if (pDevKey) {
1025                 DTappsFreeDeviceKey(&pDevKey);
1026         }
1027
1028         if (check_valid != TADC_LICENSE_VALID) {
1029                 DRM_TAPPS_EXCEPTION("DTappsGetCEK FAILED!!!, check_valid = %d", check_valid);
1030                 return FALSE;
1031         } else {
1032                 /* All fine... Allocate memory for CID and fill the value */
1033                 if ((t_RO != NULL) && (szCid != NULL)) {
1034                         t_RO -> t_Content.CID = (unsigned char *) DTAPPS_MALLOC(CID_SIZE + 1);
1035
1036                         if (NULL == t_RO -> t_Content.CID) {
1037                                 DRM_TAPPS_EXCEPTION("Memory Allocation Error!! "
1038                                                                         "t_RO->t_Content.CID = %p",
1039                                                                         t_RO -> t_Content.CID);
1040                                 check_valid = TADC_LICENSE_UNKNOWN_ERROR;
1041                                 goto DTAPPS_ERROR;
1042                         }
1043
1044                         DTAPPS_MEMSET(t_RO -> t_Content.CID, 0x00, CID_SIZE + 1);
1045                         DTAPPS_MEMCPY(t_RO -> t_Content.CID, szCid, CID_SIZE);
1046                         DRM_TAPPS_LOG("DTappsGetCEK SUCCESS!!!, check_valid = %d", check_valid);
1047                 }
1048
1049                 return TRUE;
1050         }
1051 }
1052
1053 BOOL DTapps_RIGHTS_INSTALL(const char *name,
1054                                                    const char *time,
1055 #ifdef DTAPPS_STORE_CEK_IN_DB
1056                                                    const char *cek,
1057                                                    const char *cek_hash,
1058 #endif
1059                                                    const char *constraint_buffer,
1060                                                    const char *constraint_hash,
1061                                                    const char *cid,
1062                                                    BOOL isUpdate)
1063 {
1064         void *pDb = NULL;
1065         const char *query = NULL;
1066         sqlite3_stmt *pstmt = NULL;
1067         unsigned int dIdx = 0;
1068         BOOL ret_value = FALSE;
1069
1070         DRM_TAPPS_LOG("Open DB......");
1071         ret_value = DTappsDBOpen(pDb, __func__);
1072
1073         if (ret_value != TRUE) {
1074                 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d", ret_value);
1075                 return FALSE;
1076         }
1077
1078         if (isUpdate == FALSE) {
1079                 query = "INSERT INTO dtapps2rights (name, time_t, "
1080 #ifdef DTAPPS_STORE_CEK_IN_DB
1081                                 " cek, cek_hash, "
1082 #endif
1083                                 " constraint_buffer, constraint_hash, cid) "
1084                                 " VALUES (?, ?, "
1085 #ifdef DTAPPS_STORE_CEK_IN_DB
1086                                 " ?, ?, "
1087 #endif
1088                                 " ?, ?, ?);";
1089         } else {
1090                 query = "UPDATE dtapps2rights SET name=?, time_t=?,"
1091 #ifdef DTAPPS_STORE_CEK_IN_DB
1092                                 " cek=?, cek_hash=?, "
1093 #endif
1094                                 " constraint_buffer=?, constraint_hash=? "
1095                                 " WHERE r_id=?;";
1096         }
1097
1098         DRM_TAPPS_LOG("Prepare Statement........");
1099         pstmt = (sqlite3_stmt *) DTappsStmtPrepare(pDb, query);
1100
1101         if (pstmt == NULL) {
1102                 DRM_TAPPS_EXCEPTION("Prepare Statement failed. query=%s", query);
1103                 goto Error_Exit;
1104         }
1105
1106         DRM_TAPPS_LOG("Binding Parameters........");
1107         ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR,
1108                                                                         (void *)name, strlen(name));
1109
1110         if (ret_value != TRUE) {
1111                 DRM_TAPPS_EXCEPTION("Binding Parameter(name=%s) failed. ret_value = %d", name,
1112                                                         ret_value);
1113                 goto Error_Exit;
1114         }
1115
1116         ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR,
1117                                                                         (void *)time, strlen(time));
1118
1119         if (ret_value != TRUE) {
1120                 DRM_TAPPS_EXCEPTION("Binding Parameter(time=%s) failed. ret_value = %d", time,
1121                                                         ret_value);
1122                 goto Error_Exit;
1123         }
1124
1125 #ifdef DTAPPS_STORE_CEK_IN_DB
1126         ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR,
1127                                                                         (void *)cek, strlen(cek));
1128
1129         if (ret_value != TRUE) {
1130                 DRM_TAPPS_EXCEPTION("Binding Parameter(cek=%s) failed. ret_value = %d", cek,
1131                                                         ret_value);
1132                 goto Error_Exit;
1133         }
1134
1135         ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR,
1136                                                                         (void *)cek_hash, strlen(cek_hash));
1137
1138         if (ret_value != TRUE) {
1139                 DRM_TAPPS_EXCEPTION("Binding Parameter(cek_hash=%s) failed. ret_value = %d",
1140                                                         cek_hash, ret_value);
1141                 goto Error_Exit;
1142         }
1143
1144 #endif
1145
1146         ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR,
1147                                                                         (void *)constraint_buffer, strlen(constraint_buffer));
1148
1149         if (ret_value != TRUE) {
1150                 DRM_TAPPS_EXCEPTION("Binding Parameter(constraint_buffer=%s) failed. "
1151                                                         "ret_value = %d",
1152                                                         constraint_buffer, ret_value);
1153                 goto Error_Exit;
1154         }
1155
1156         ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR,
1157                                                                         (void *)constraint_hash, strlen(constraint_hash));
1158
1159         if (ret_value != TRUE) {
1160                 DRM_TAPPS_EXCEPTION("Binding Parameter(constraint_hash=%s) failed. "
1161                                                         "ret_value = %d",
1162                                                         constraint_hash, ret_value);
1163                 goto Error_Exit;
1164         }
1165
1166         ret_value = DTappsStmtBindParam(pstmt, dIdx++, TAPPSDB_TYPE_VARCHAR,
1167                                                                         (void *)cid, strlen(cid));
1168
1169         if (ret_value != TRUE) {
1170                 DRM_TAPPS_EXCEPTION("Binding Parameter(cid=%s) failed. ret_value = %d", cid,
1171                                                         ret_value);
1172                 goto Error_Exit;
1173         }
1174
1175         DRM_TAPPS_LOG("Begin Transaction........");
1176         ret_value = DTappsDBBeginImmedTrans(__func__);
1177
1178         if (ret_value != TRUE) {
1179                 DRM_TAPPS_EXCEPTION("DB Begin Transaction ret_value = %d", ret_value);
1180                 goto Error_Exit;
1181         }
1182
1183         DRM_TAPPS_LOG("Execute SQL to Insert Contents into Table........");
1184         ret_value = DTappsStmtExecute(pstmt);
1185
1186         if (ret_value != TRUE) {
1187                 DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! ret_value = %d", ret_value);
1188                 goto Error_Exit;
1189         }
1190
1191         DRM_TAPPS_LOG("Commit DB........");
1192         ret_value = DTappsDBCommit(__func__);
1193
1194         if (ret_value != TRUE) {
1195                 DRM_TAPPS_EXCEPTION("Commit DB Failed!! ret_value = %d", ret_value);
1196                 goto Error_Exit;
1197         }
1198
1199         DRM_TAPPS_LOG("Release Statement........");
1200         ret_value = DTappsStmtRelease(pstmt);
1201
1202         if (ret_value != TRUE) {
1203                 DRM_TAPPS_EXCEPTION("Release Statement Failed!! ret_value = %d", ret_value);
1204                 pstmt = NULL;
1205                 goto Error_Exit;
1206         }
1207
1208         DRM_TAPPS_LOG("Close DB........");
1209         ret_value = DTappsDBClose(__func__);
1210
1211         if (ret_value != TRUE) {
1212                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d", ret_value);
1213                 goto Error_Exit;
1214         }
1215
1216         DRM_TAPPS_LOG("Install DB Operartion Successful!!!");
1217         return TRUE;
1218
1219 Error_Exit:
1220
1221         if (pstmt != NULL)
1222                 DTappsStmtRelease(pstmt);
1223
1224         ret_value = DTappsDBRollback(__func__);
1225
1226         if (ret_value != TRUE) {
1227                 DRM_TAPPS_EXCEPTION("Rollback DB Failed!! ret_value = %d", ret_value);
1228         }
1229
1230         ret_value = DTappsDBClose(__func__);
1231
1232         if (ret_value != TRUE) {
1233                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d", ret_value);
1234         }
1235
1236         return FALSE;
1237 }
1238
1239 BOOL DTapps_RIGHTS_SELECT_ONE(const char *cid, DTAPPS_RIGHTS_ROW *row)
1240 {
1241         void *pDb = NULL;
1242         const char *query = NULL;
1243         sqlite3_stmt *pstmt = NULL;
1244         unsigned int dIdx = 0;
1245         BOOL ret_value = FALSE;
1246         int db_ret = -1;
1247         DTAPPS_RIGHTS_ROW *result = row;
1248
1249         char *name, *time, *constraint_buffer, *constraint_hash;
1250 #ifdef DTAPPS_STORE_CEK_IN_DB
1251         char *cek, *cek_hash;
1252 #endif
1253
1254         DRM_TAPPS_LOG("Open DB......");
1255         ret_value = DTappsDBOpen(pDb, __func__);
1256
1257         if (ret_value != TRUE) {
1258                 DRM_TAPPS_EXCEPTION("DB Open Failed!! ret_value = %d", ret_value);
1259                 return FALSE;
1260         }
1261
1262         query = "SELECT r_id, name, time_t, "
1263 #ifdef DTAPPS_STORE_CEK_IN_DB
1264                         " cek, cek_hash, "
1265 #endif
1266                         " constraint_buffer, constraint_hash "
1267                         " FROM dtapps2rights"
1268                         " WHERE cid=? ORDER BY r_id desc limit 1;";
1269
1270
1271         DRM_TAPPS_LOG("Begin Transaction........");
1272         ret_value = DTappsDBBeginImmedTrans(__func__);
1273
1274         if (ret_value != TRUE) {
1275                 DRM_TAPPS_EXCEPTION("DB Begin Transaction Failed!! ret_value = %d", ret_value);
1276                 goto Error_Exit;
1277         }
1278
1279         DRM_TAPPS_LOG("Prepare Statement........");
1280         db_ret = sqlite3_prepare((sqlite3 *)pDb, query, -1, &pstmt, NULL);
1281
1282         if (pstmt == NULL) {
1283                 DRM_TAPPS_EXCEPTION("Prepare Statement failed. query=%s, error=%s", query,
1284                                                         sqlite3_errmsg((sqlite3 *)pDb));
1285                 goto Error_Exit;
1286         }
1287
1288         DRM_TAPPS_LOG("Binding Parameters........");
1289         db_ret = sqlite3_bind_text(pstmt, ++dIdx, cid, strlen(cid), SQLITE_TRANSIENT);
1290
1291         if (db_ret != SQLITE_OK) {
1292                 DRM_TAPPS_EXCEPTION("Binding Parameter(cid=%s) failed. "
1293                                                         "ret_value = %d, query=%s",
1294                                                         cid, db_ret, query);
1295                 goto Error_Exit;
1296         }
1297
1298         DRM_TAPPS_LOG("Execute SQL to Select Contents from Table........");
1299         db_ret = sqlite3_step(pstmt);
1300
1301         if (db_ret == SQLITE_OK || db_ret == SQLITE_DONE) {
1302                 DRM_TAPPS_FRQ_LOG("Executed SQL Select Query. But no row was returned");
1303                 goto Error_Exit;
1304         } else if (db_ret != SQLITE_ROW) {
1305                 DRM_TAPPS_EXCEPTION("Execute SQL Query Failed!! "
1306                                                         "ret_value = %d, query=%s, error=%s",
1307                                                         db_ret, query, sqlite3_errmsg((sqlite3 *)pDb));
1308                 goto Error_Exit;
1309         }
1310
1311         memset(result, 0, sizeof(DTAPPS_RIGHTS_ROW));
1312
1313         dIdx = 0;
1314         result->r_id = sqlite3_column_int(pstmt, dIdx++);
1315         DRM_TAPPS_LOG("....SEELECTED : r_id = %d", result->r_id);
1316
1317         name = (char *) sqlite3_column_text(pstmt, dIdx++);
1318
1319         if (name != NULL)
1320                 memcpy(result->name, name, strlen(name) + 1);
1321
1322         DRM_TAPPS_LOG("....SEELECTED : name = %s", result->name);
1323
1324         time = (char *) sqlite3_column_text(pstmt, dIdx++);
1325
1326         if (time != NULL)
1327                 memcpy(result->time, time, strlen(time) + 1);
1328
1329         DRM_TAPPS_LOG("....SEELECTED : time = %s", result->time);
1330
1331 #ifdef DTAPPS_STORE_CEK_IN_DB
1332         cek = (char *) sqlite3_column_text(pstmt, dIdx++);
1333
1334         if (cek != NULL)
1335                 memcpy(result->cek, cek, strlen(cek) + 1);
1336
1337         DRM_TAPPS_LOG("....SEELECTED : cek = %s", result->cek);
1338
1339         cek_hash = (char *) sqlite3_column_text(pstmt, dIdx++);
1340
1341         if (cek_hash != NULL)
1342                 memcpy(result->cek_hash, cek_hash, strlen(cek_hash) + 1);
1343
1344         DRM_TAPPS_LOG("....SEELECTED : cek_hash = %s", result->cek_hash);
1345 #endif
1346
1347         constraint_buffer = (char *) sqlite3_column_text(pstmt, dIdx++);
1348
1349         if (constraint_buffer != NULL)
1350                 memcpy(result->constraint_buffer, constraint_buffer, strlen(constraint_buffer) + 1);
1351
1352         DRM_TAPPS_LOG("....SEELECTED : constraint_buffer = %s",
1353                                   result->constraint_buffer);
1354
1355         constraint_hash = (char *) sqlite3_column_text(pstmt, dIdx++);
1356
1357         if (constraint_hash != NULL)
1358                 memcpy(result->constraint_hash, constraint_hash, strlen(constraint_hash) + 1);
1359
1360         DRM_TAPPS_LOG("....SEELECTED : constraint_hash = %s", result->constraint_hash);
1361
1362         memcpy(result->cid, cid, strlen(cid) + 1);
1363         DRM_TAPPS_LOG("....SEELECTED : cid = %s", result->cid);
1364
1365         DRM_TAPPS_LOG("Release Statement........");
1366         ret_value = DTappsStmtRelease(pstmt);
1367
1368         if (ret_value != TRUE) {
1369                 DRM_TAPPS_EXCEPTION("Release Statement Failed!! ret_value = %d", ret_value);
1370                 pstmt = NULL;
1371                 goto Error_Exit;
1372         }
1373
1374         DRM_TAPPS_LOG("Close DB........");
1375         ret_value = DTappsDBClose(__func__);
1376
1377         if (ret_value != TRUE) {
1378                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d", ret_value);
1379                 goto Error_Exit;
1380         }
1381
1382         DRM_TAPPS_LOG("Select a DB row Operartion Successful!!!");
1383         return TRUE;
1384
1385 Error_Exit:
1386
1387         if (pstmt)
1388                 DTappsStmtRelease(pstmt);
1389
1390         ret_value = DTappsDBClose(__func__);
1391
1392         if (ret_value != TRUE) {
1393                 DRM_TAPPS_EXCEPTION("Close DB Failed!! ret_value = %d", ret_value);
1394         }
1395
1396         return FALSE;
1397 }