Migrate to openssl 1.1
[platform/core/security/drm-service-core-tizen.git] / test / drm_testutil.cpp
1 /*
2  * Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <time.h>
21 #include <vector>
22 #include <fstream>
23 #include <iostream>
24
25 #include <openssl/bio.h>
26 #include <openssl/evp.h>
27 #include <openssl/buffer.h>
28 #include <openssl/bn.h>
29 #include <openssl/crypto.h>
30 #include <openssl/err.h>
31 #include <openssl/pem.h>
32 #include <openssl/rsa.h>
33 #include <openssl/aes.h>
34 #include <openssl/sha.h>
35
36 #include <TADC_IF.h>
37 #include <drm-tizen-apps.h>
38 #include <TADC_Util.h>
39
40 #include "drm_testutil.h"
41
42 #define _INITIALIZED 0
43 #define _UNINITIALIZED -1
44
45 using Binary = std::vector<unsigned char>;
46
47 static unsigned char g_baAESKey[32] = {
48         0xf8, 0x87, 0x0a, 0xc5, 0xd3, 0x6d, 0x44, 0x49,
49         0x03, 0x9f, 0xbd, 0x1e, 0xa8, 0x2f, 0xf6, 0xc3,
50         0xdf, 0x3b, 0x02, 0x13, 0x58, 0x1b, 0x12, 0x30,
51         0x1c, 0xd7, 0xad, 0xa5, 0x1f, 0x5d, 0x01, 0x33
52 };
53
54
55 int __get_random_bytes(char *output, int random_len)
56 {
57         FILE *fp = fopen("/dev/urandom", "r");
58         auto size = fread(output, 1, random_len, fp);
59
60         fclose(fp);
61
62         if (size != static_cast<size_t>(random_len))
63                 return DRMTEST_ERR_IO;
64
65         return DRMTEST_SUCCESS;
66 }
67
68 int __file_copy(const char *target_path, const char *source_path)
69 {
70         int ret = DRMTEST_SUCCESS;
71         FILE *source = NULL;
72         FILE *target = NULL;
73         size_t l1, l2;
74         unsigned char buffer[8192];
75
76         source = fopen(source_path, "r");
77
78         if (source == NULL) {
79                 ret = DRMTEST_ERR_IO;
80                 goto error;
81         }
82
83         target = fopen(target_path, "w");
84
85         if (target == NULL) {
86                 ret = DRMTEST_ERR_IO;
87                 goto error;
88         }
89
90         while ((l1 = fread(buffer, 1, sizeof buffer, source)) > 0) {
91                 l2 = fwrite(buffer, 1, l1, target);
92
93                 if (l2 < l1) {
94                         if (ferror(target)) {
95                                 ret = DRMTEST_ERR_IO;
96                                 goto error;
97                         }
98                 }
99         }
100
101 error:
102
103         if (source != NULL)
104                 fclose(source);
105
106         if (target != NULL)
107                 fclose(target);
108
109         return ret;
110 }
111
112 void _base64_encode(const unsigned char *input, int length, char **output)
113 {
114         *output = Base64Encode((unsigned char *)input, length);
115 }
116
117 void _base64_decode(const char *input, unsigned char **output, int *out_len)
118 {
119         *output = Base64Decode((char *)input, out_len);
120 }
121
122 char *_replace_all(char *s, const char *olds, const char *news)
123 {
124         char *result = NULL, *sr = NULL;
125         size_t i, count = 0;
126         size_t oldlen = strlen(olds);
127
128         if (oldlen < 1) return s;
129
130         size_t newlen = strlen(news);
131
132         if (newlen != oldlen) {
133                 for (i = 0; s[i] != '\0';) {
134                         if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
135                         else i++;
136                 }
137         } else {
138                 i = strlen(s);
139         }
140
141         result = (char *) malloc(i + 1 + count * (newlen - oldlen));
142
143         if (result == NULL) return NULL;
144
145         sr = result;
146
147         while (*s) {
148                 if (memcmp(s, olds, oldlen) == 0) {
149                         memcpy(sr, news, newlen);
150                         sr += newlen;
151                         s  += oldlen;
152                 } else {
153                         *sr++ = *s++;
154                 }
155         }
156
157         *sr = '\0';
158
159         return result;
160 }
161
162 Binary _read_ro_file(const char *filename)
163 {
164         try {
165                 std::ifstream is(filename, std::ifstream::binary);
166
167                 if (!is || !is.is_open())
168                         return Binary();
169
170                 is.seekg(0, is.end);
171                 int length = is.tellg();
172                 is.seekg(0, is.beg);
173
174                 Binary buffer(length + 1); /* for null-terminated */
175
176                 is.read(reinterpret_cast<char *>(buffer.data()), length);
177
178                 if (!is)
179                         return Binary();
180
181                 return buffer;
182         } catch (...) {
183                 std::cerr << "Failed to read ro file: " << filename << std::endl;
184                 return Binary();
185         }
186 }
187
188 int _create_dh_key(const char *dh_key_p_hex, const char *dh_key_g_hex,
189                                    DH **ppkey)
190 {
191         int ret = DRMTEST_SUCCESS;
192         DH *pDH = NULL;
193         BIGNUM *p = NULL, *g = NULL;
194
195         if ((pDH = DH_new()) == NULL) {
196                 printf("...FAIL: DH_new() error");
197                 ret = DRMTEST_ERR_CRYPTO;
198                 goto error;
199         }
200
201         BN_hex2bn(&p, dh_key_p_hex);
202         BN_hex2bn(&g, dh_key_g_hex);
203         DH_set0_pqg(pDH, p, nullptr, g);
204
205         /* Set a to run with normal modexp and b to use constant time */
206         DH_clear_flags(pDH, DH_FLAG_NO_EXP_CONSTTIME);
207
208         // Generate DH Key
209         if (!DH_generate_key(pDH)) {
210                 printf("...FAIL: DH_generate_key");
211                 ret = DRMTEST_ERR_CRYPTO;
212                 goto error;
213         }
214
215         *ppkey = pDH;
216 error:
217
218         if (ret != DRMTEST_SUCCESS && pDH != NULL)
219                 DH_free(pDH);
220
221         return ret;
222 }
223
224 int _free_dh_key(DH *pkey)
225 {
226         if (pkey != NULL)
227                 DH_free(pkey);
228
229         return DRMTEST_SUCCESS;
230 }
231
232
233 int _get_dh_hex_pubkey(const DH *pkey, char **dh_pubkey)
234 {
235         *dh_pubkey = BN_bn2hex(DH_get0_pub_key(pkey));
236         return DRMTEST_SUCCESS;
237 }
238
239 int _get_dh_shared_secret_key(const char *dh_hex_pubkey, DH *pkey,
240                                                           unsigned char **dh_shared_secret_key, int *dh_sec_key_len)
241 {
242         int ret = DRMTEST_SUCCESS;
243
244         BIGNUM  *pPubKey = NULL;
245         unsigned char *secret_key_buff = NULL;
246         unsigned char tmp_buff[DH_size(pkey)] = { 0, };
247
248         BN_hex2bn(&pPubKey, dh_hex_pubkey);
249
250         if (DH_compute_key(tmp_buff, pPubKey, pkey) < 0) {
251                 ret = DRMTEST_ERR_CRYPTO;
252                 goto error;
253         }
254
255         secret_key_buff = (unsigned char *) malloc(DH_size(pkey) / 2);
256
257         if (secret_key_buff == NULL) {
258                 ret = DRMTEST_ERR_CRYPTO;
259                 goto error;
260         }
261
262         memset(secret_key_buff, 0, DH_size(pkey) / 2);
263
264         printf("APP_DRM TEST: shared secret : ");
265
266         for (int i = 0; i < (DH_size(pkey) / 2); i++) {
267                 secret_key_buff[i] = tmp_buff[i * 2] ^ tmp_buff[(i * 2) + 1];
268                 printf("%02x", secret_key_buff[i]);
269         }
270
271         printf("\n");
272
273         *dh_shared_secret_key = secret_key_buff;
274         *dh_sec_key_len = DH_size(pkey) / 2;
275 error:
276
277         if (pPubKey != NULL)
278                 BN_free(pPubKey);
279
280         if (ret != DRMTEST_SUCCESS && secret_key_buff != NULL)
281                 free(secret_key_buff);
282
283         return ret;
284 }
285
286
287 int _create_right_object_without_signature(const char *ro_template_path,
288                 const char *cid, const char *duid,
289                 char **ro_buff)
290 {
291         int ret = DRMTEST_SUCCESS;
292         char *cid_filled = NULL;
293         char *duid_filled = NULL;
294
295         auto buf = _read_ro_file(ro_template_path);
296
297         cid_filled = _replace_all(reinterpret_cast<char *>(buf.data()),
298                                                           STR_PLACE_HOLDER_CID, cid);
299         duid_filled = _replace_all(cid_filled, STR_PLACE_HOLDER_DUID, duid);
300
301         *ro_buff = duid_filled;
302
303         if (cid_filled != NULL)
304                 free(cid_filled);
305
306         if (ret != DRMTEST_SUCCESS && duid_filled != NULL)
307                 free(duid_filled);
308
309         return ret;
310 }
311
312 int _create_ro_signature(const char *ro_buff, const char *signer_prikey_path,
313                                                  char **signature)
314 {
315         int ret = DRMTEST_SUCCESS;
316
317         EVP_PKEY *pkey = NULL;
318         RSA      *prsa = NULL;
319         const char *END_STR = "</CertificateChain>";
320         const char *tmp_end_hash_input = NULL;
321         int hash_input_size = 0;
322         unsigned char hash_value[20];
323         unsigned char sig_value[MAX_CERT_SIZE]; // enough buffer
324         unsigned int sig_len = 0;
325         char *b64_sig_value = NULL;
326
327         FILE *file = NULL;
328
329         // get private key
330         file = fopen(signer_prikey_path, "r");
331
332         if (file == NULL) {
333                 ret = DRMTEST_ERR_IO;
334                 goto error;
335         }
336
337         pkey = PEM_read_PrivateKey(file, &pkey, NULL, NULL);
338
339         if (pkey == NULL) {
340                 ret = DRMTEST_ERR_IO;
341                 goto error;
342         }
343
344         prsa = EVP_PKEY_get1_RSA(pkey);
345
346         if (prsa == NULL) {
347                 ret = DRMTEST_ERR_CRYPTO;
348                 goto error;
349         }
350
351         // get hash input size
352         tmp_end_hash_input = strstr(ro_buff, END_STR);
353         hash_input_size = (tmp_end_hash_input - ro_buff) + strlen(END_STR);
354
355         // get hash value
356         SHA_CTX     alginfo;
357         SHA1_Init(&alginfo);
358         SHA1_Update(&alginfo, ro_buff, hash_input_size);
359         SHA1_Final(hash_value, &alginfo);
360
361         // get signature value
362         if (1 != RSA_sign(NID_sha1, hash_value, 20, sig_value, &sig_len, prsa)) {
363                 ret = DRMTEST_ERR_CRYPTO;
364                 goto error;
365         }
366
367         // convert to base64 string
368         _base64_encode(sig_value, (int) sig_len, &b64_sig_value);
369
370         *signature = b64_sig_value;
371 error:
372
373         if (file != NULL)
374                 fclose(file);
375
376         if (pkey != NULL)
377                 EVP_PKEY_free(pkey);
378
379         if (ret != DRMTEST_SUCCESS && b64_sig_value != NULL)
380                 free(b64_sig_value);
381
382         return ret;
383 }
384
385 int _add_signature_to_ro(const char *ro_buff, const char *signature,
386                                                  char **ro_with_signature)
387 {
388         int ret = DRMTEST_SUCCESS;
389         char *buff = NULL;
390         buff = _replace_all((char *)ro_buff, STR_PLACE_HOLDER_SIGNATURE, signature);
391         *ro_with_signature = buff;
392         return ret;
393 }
394
395 int _encrypt_ro_with_dh_sec_key(const char *ro_with_signature,
396                                                                 const unsigned char *dh_secret_key, const int dh_sec_key_len,
397                                                                 char **encrypted_ro)
398 {
399         int ret = DRMTEST_SUCCESS;
400         TADC_U8     key[16] = {0, };
401         TADC_U8     iv[16] = {0, };
402         int encrypted_len = 0;
403         unsigned char encrypted_buff[MAX_CERT_SIZE] = {0, };;
404
405         (void) dh_sec_key_len; // to prevent unused varialbe error
406
407         TADC_IF_MemCpy(key, dh_secret_key, 16);
408         TADC_IF_MemCpy(iv, (dh_secret_key + 16), 16);
409         ret = TADC_IF_AES_CTR(key, 16, iv, strlen(ro_with_signature),
410                                                   (unsigned char *)ro_with_signature,
411                                                   &encrypted_len, (unsigned char *)encrypted_buff);
412
413         if (ret != 0) {
414                 ret = DRMTEST_ERR_CRYPTO;
415                 goto error;
416         }
417
418         _base64_encode(encrypted_buff, encrypted_len, encrypted_ro);
419
420 error:
421         return ret;
422 }
423
424 int _create_response_data_in_ro_response(const char *reqid,
425                 const char *encrypted_ro, const char *dh_pubkey,
426                 char **response_data)
427 {
428         int ret = DRMTEST_SUCCESS;
429         char tmp_buff[MAX_CERT_SIZE] = { 0, };
430         unsigned char hashed_reqid[20] = { 0, };
431         char hex_hashed_reqid[256] = { 0, };
432         unsigned char hash_value[20] = { 0, };
433         int hmac_len = 0;
434         unsigned char hmac[1024 * 10] = { 0, };
435         char *hmac_base64 = NULL;
436         char *resp_data = NULL;
437
438         // get hashed req_id
439         SHA_CTX alginfoForReqId;
440         SHA1_Init(&alginfoForReqId);
441         SHA1_Update(&alginfoForReqId, reqid, strlen(reqid));
442         SHA1_Final(hashed_reqid, &alginfoForReqId);
443
444         for (size_t i = 0; i < sizeof(hashed_reqid); i++)
445                 sprintf(hex_hashed_reqid + i * 2, "%02x", hashed_reqid[i]);
446
447         sprintf(tmp_buff, "reqid=%s;B=%s;license=%s", hex_hashed_reqid, dh_pubkey,
448                         encrypted_ro);
449
450         // get hash value
451         SHA_CTX     alginfo;
452         SHA1_Init(&alginfo);
453         SHA1_Update(&alginfo, tmp_buff, strlen(tmp_buff));
454         SHA1_Final(hash_value, &alginfo);
455
456         // encrypt hash value
457         TADC_U8     key[16] = {0, };
458         TADC_U8     iv[16] = {0, };
459
460         TADC_IF_MemCpy(key, g_baAESKey, 16);
461         TADC_IF_MemCpy(iv, (g_baAESKey + 16), 16);
462         ret = TADC_IF_AES_CTR(key, 16, iv, 20, hash_value, &hmac_len, hmac);
463
464         if (ret != 0) {
465                 ret = DRMTEST_ERR_CRYPTO;
466                 goto error;
467         }
468
469         // base64 encode
470         _base64_encode(hmac, 20, &hmac_base64);
471
472         // add hmac
473         strncat(tmp_buff, ";hmac=", strlen(";hmac="));
474         strncat(tmp_buff, hmac_base64, strlen(hmac_base64));
475
476         // make return value
477         resp_data = (char *) malloc(strlen(tmp_buff) + 1);
478
479         if (resp_data == NULL) {
480                 ret = DRMTEST_ERR_MEMORY;
481                 goto error;
482         }
483
484         memset(resp_data, 0, strlen(tmp_buff) + 1);
485         strncpy(resp_data, tmp_buff, strlen(tmp_buff));
486
487         *response_data = resp_data;
488
489 error:
490
491         if (hmac_base64 != NULL)
492                 free(hmac_base64);
493
494         if (ret != DRMTEST_SUCCESS && resp_data != NULL)
495                 free(resp_data);
496
497         return ret;
498 }
499
500 int _create_time_stamp(const unsigned char *dh_secret_key, char **time_stamp)
501 {
502         int ret = DRMTEST_SUCCESS;
503
504         char tmp_time_buff[128] = {0, };
505         unsigned char enc_time_buff[512] = {0, };
506         char *time_base64 = NULL;
507         int enc_time_buff_len = 0;
508         time_t now = time(NULL);
509         const struct tm *gt = gmtime(&now);
510
511         sprintf(tmp_time_buff, "%d-%d-%dT%d:%d:00:Z",
512                         gt->tm_year + 1900, gt->tm_mon + 1, gt->tm_mday,
513                         gt->tm_hour + 1, gt->tm_min + 1);
514
515         // encrypt time_stamp
516         TADC_U8 key[16] = {0, };
517         TADC_U8 iv[16] = {0, };
518
519         TADC_IF_MemCpy(key, dh_secret_key, 16);
520         TADC_IF_MemCpy(iv, (dh_secret_key + 16), 16);
521         ret = TADC_IF_AES_CTR(key, 16, iv, strlen(tmp_time_buff),
522                                                   (unsigned char *)tmp_time_buff,
523                                                   &enc_time_buff_len, enc_time_buff);
524
525         if (ret != 0) {
526                 ret = DRMTEST_ERR_CRYPTO;
527                 goto error;
528         }
529
530         // convert to base64
531         _base64_encode(enc_time_buff, enc_time_buff_len, &time_base64);
532
533         *time_stamp = time_base64;
534 error:
535
536         if (ret != DRMTEST_SUCCESS && time_base64 != NULL)
537                 free(time_base64);
538
539         return ret;
540 }
541
542 int generate_purchase_response(char **purchase_response_buff, char **req_id)
543 {
544         int ret = DRMTEST_SUCCESS;
545         char resp_buff[1024 * 5] = {0, };
546         char *resp = NULL;
547         char *rid = NULL;
548         char random[64] = {0, };
549
550         const char *format1 = "<?xml version=\"1.0\">\n";
551         const char *format2 = "<response result=\"0\" message=\"\">\n";
552         const char *format3 = "    <DRMType>%d</DRMType>\n";
553         const char *format4 = "    <riurl>%s</riurl>\n";
554         const char *format5 = "    <reqid>%s</reqid>\n";
555         const char *format6 = "</response>";
556
557         resp = (char *) malloc(sizeof(resp_buff));
558
559         if (resp == NULL) {
560                 ret = DRMTEST_ERR_MEMORY;
561                 goto error;
562         }
563
564         memset(resp, 0, sizeof(resp_buff));
565
566         __get_random_bytes(random, sizeof(random));
567         rid = (char *) malloc(1024);
568
569         if (rid == NULL) {
570                 ret = DRMTEST_ERR_MEMORY;
571                 goto error;
572         }
573
574         memset(rid, 0, 1024);
575
576         for (size_t i = 0; i < sizeof(random); i++)
577                 sprintf(rid + i * 2, "%02x", random[i]);
578
579         strncat(resp_buff, format1, strlen(format1));
580         strncat(resp_buff, format2, strlen(format2));
581         strncat(resp_buff, format3, strlen(format3));
582         strncat(resp_buff, format4, strlen(format4));
583         strncat(resp_buff, format5, strlen(format5));
584         strncat(resp_buff, format6, strlen(format6));
585
586         sprintf(resp, resp_buff, 1, RIURL, rid);
587
588         *purchase_response_buff = resp;
589         *req_id = rid;
590
591 error:
592
593         if (ret != DRMTEST_SUCCESS && resp != NULL)
594                 free(resp);
595
596         if (ret != DRMTEST_SUCCESS && rid != NULL)
597                 free(rid);
598
599         return ret;
600 }
601
602 int generate_right_object_request(const char *license_response_buff)
603 {
604         int ret = DRMTEST_SUCCESS;
605         unsigned int  req_buff_len = 1024 * 5;
606         char url_buff[1024] = {0, };
607         unsigned int  url_buff_len = sizeof(url_buff);
608         char *req_buff = NULL;
609
610         req_buff = (char *)malloc(1024 * 5);
611
612         if (req_buff == NULL) {
613                 ret = DRMTEST_ERR_MEMORY;
614                 goto error;
615         }
616
617         memset(req_buff, 0, req_buff_len);
618
619         ret = drm_tizen_generate_license_request(license_response_buff,
620                         strlen(license_response_buff),
621                         req_buff, &req_buff_len, url_buff, &url_buff_len);
622
623         if (ret != 1) {
624                 ret = DRMTEST_ERR_TIZDRM;
625                 goto error;
626         }
627
628 error:
629
630         if (ret != DRMTEST_SUCCESS && req_buff != NULL)
631                 free(req_buff);
632
633         return ret;
634 }
635
636
637 int get_dh_key_from_ro_request(const char *ro_request_buff,
638                                                            char **dh_key_p, char **dh_key_g, char **dh_key_a)
639 {
640         int ret = DRMTEST_SUCCESS;
641
642         const char *PFX_P = "p=";
643         const char *PFX_G = ";g=";
644         const char *PFX_A = ";A=";
645         const char *PFX_HMAC = ";hmac=";
646
647         const char *idx_p = strstr(ro_request_buff, PFX_P);
648         const char *idx_g = strstr(ro_request_buff, PFX_G);
649         const char *idx_a = strstr(ro_request_buff, PFX_A);
650         const char *idx_hmac = strstr(ro_request_buff, PFX_HMAC);
651
652         int len_p = idx_g - idx_p - strlen(PFX_P);
653         int len_g = idx_a - idx_g - strlen(PFX_G);
654         int len_a = idx_hmac - idx_a - strlen(PFX_A);
655
656         char *buff_p = NULL;
657         char *buff_g = NULL;
658         char *buff_a = NULL;
659
660         buff_p = (char *)malloc(len_p + 1);
661
662         if (buff_p == NULL) {
663                 ret = DRMTEST_ERR_MEMORY;
664                 goto error;
665         }
666
667         memset(buff_p, 0, len_p + 1);
668         strncpy(buff_p, idx_p + strlen(PFX_P), len_p);
669         *dh_key_p = buff_p;
670
671         buff_g = (char *)malloc(len_g + 1);
672
673         if (buff_g == NULL) {
674                 ret = DRMTEST_ERR_MEMORY;
675                 goto error;
676         }
677
678         memset(buff_g, 0, len_g + 1);
679         strncpy(buff_g, idx_g + strlen(PFX_G), len_g);
680         *dh_key_g = buff_g;
681
682         buff_a = (char *)malloc(len_a + 1);
683
684         if (buff_a == NULL) {
685                 ret = DRMTEST_ERR_MEMORY;
686                 goto error;
687         }
688
689         memset(buff_a, 0, len_a + 1);
690         strncpy(buff_a, idx_a + strlen(PFX_A), len_a);
691         *dh_key_a = buff_a;
692
693 error:
694
695         if (ret != DRMTEST_SUCCESS && buff_p != NULL)
696                 free(buff_p);
697
698         if (ret != DRMTEST_SUCCESS && buff_g != NULL)
699                 free(buff_g);
700
701         if (ret != DRMTEST_SUCCESS && buff_a != NULL)
702                 free(buff_a);
703
704         return ret;
705 }
706
707 int generate_right_object_response(const char *dh_key_p, const char *dh_key_g,
708                                                                    const char *dh_key_a,
709                                                                    const char *req_id, const char *cid,
710                                                                    const char *ro_template_path,
711                                                                    const char *duid, char **ro_response_buff)
712 {
713         int ret = DRMTEST_SUCCESS;
714
715         DH *pkey = NULL;
716         char *dh_pubkey = NULL;
717         unsigned char *dh_shared_secret_key = NULL;
718         int   dh_sec_key_len = 0;
719         char *ro_buff = NULL;
720         char *ro_signature = NULL;
721         char *ro_with_signature = NULL;
722         char *encrypted_ro = NULL;
723         char *response_data = NULL;
724         char *time_stamp = NULL;
725         char *ro_resp_buff = NULL;
726         int   ro_resp_buff_len = 0;
727
728         const char *format = "<?xml version=\"1.0\">\n"
729                                                  "<response result=\"0\" message=\"\">\n"
730                                                  "    <responsedata>%s</responsedata>\n"
731                                                  "    <timeStamp>%s</timeStamp>\n"
732                                                  "</response>";
733
734         ret = _create_dh_key(dh_key_p, dh_key_g, &pkey);
735
736         if (ret != DRMTEST_SUCCESS) {
737                 goto error;
738         }
739
740         ret = _get_dh_hex_pubkey(pkey, &dh_pubkey);
741
742         if (ret != DRMTEST_SUCCESS) {
743                 goto error;
744         }
745
746         ret = _get_dh_shared_secret_key(dh_key_a, pkey, &dh_shared_secret_key,
747                                                                         &dh_sec_key_len);
748
749         if (ret != DRMTEST_SUCCESS) {
750                 goto error;
751         }
752
753         ret = _create_right_object_without_signature(ro_template_path, cid, duid,
754                         &ro_buff);
755
756         if (ret != DRMTEST_SUCCESS) {
757                 goto error;
758         }
759
760         ret = _create_ro_signature(ro_buff, RO_ISSUER_SIGNER_KEY_FILE, &ro_signature);
761
762         if (ret != DRMTEST_SUCCESS) {
763                 goto error;
764         }
765
766         ret = _add_signature_to_ro(ro_buff, ro_signature, &ro_with_signature);
767
768         if (ret != DRMTEST_SUCCESS) {
769                 goto error;
770         }
771
772         //printf("...right object:\n%s\n", ro_with_signature);
773
774         ret = _encrypt_ro_with_dh_sec_key(ro_with_signature, dh_shared_secret_key,
775                                                                           dh_sec_key_len,
776                                                                           &encrypted_ro);
777
778         if (ret != DRMTEST_SUCCESS) {
779                 goto error;
780         }
781
782         ret = _create_response_data_in_ro_response(req_id, encrypted_ro, dh_pubkey,
783                         &response_data);
784
785         if (ret != DRMTEST_SUCCESS) {
786                 goto error;
787         }
788
789         ret = _create_time_stamp(dh_shared_secret_key, &time_stamp);
790
791         if (ret != DRMTEST_SUCCESS) {
792                 goto error;
793         }
794
795         ro_resp_buff_len = strlen(format) + strlen(response_data) + strlen(
796                                                    time_stamp) + 1;
797         ro_resp_buff = (char *) malloc(ro_resp_buff_len);
798
799         if (ro_resp_buff == NULL) {
800                 ret = DRMTEST_ERR_MEMORY;
801                 goto error;
802         }
803
804         memset(ro_resp_buff, 0, ro_resp_buff_len);
805         sprintf(ro_resp_buff, format, response_data, time_stamp);
806
807         *ro_response_buff = ro_resp_buff;
808
809 error:
810
811         if (pkey != NULL)
812                 _free_dh_key(pkey);
813
814         if (dh_pubkey != NULL)
815                 free(dh_pubkey);
816
817         if (dh_shared_secret_key != NULL)
818                 free(dh_shared_secret_key);
819
820         if (ro_buff != NULL)
821                 free(ro_buff);
822
823         if (ro_signature != NULL)
824                 free(ro_signature);
825
826         if (ro_with_signature != NULL)
827                 free(ro_with_signature);
828
829         if (encrypted_ro != NULL)
830                 free(encrypted_ro);
831
832         if (response_data != NULL)
833                 free(response_data);
834
835         if (time_stamp != NULL)
836                 free(time_stamp);
837
838         if (ret != DRMTEST_SUCCESS && ro_resp_buff != NULL)
839                 free(ro_resp_buff);
840
841         return ret;
842 }
843
844
845 int is_identical_files(const char *file1, const char *file2, int *identical)
846 {
847         int ret = DRMTEST_SUCCESS;
848
849         FILE *fp1 = NULL, *fp2 = NULL;
850         int ch1, ch2;
851
852         fp1 = fopen(file1, "r");
853
854         if (fp1 == NULL) {
855                 ret = DRMTEST_ERR_IO;
856                 goto error;
857         }
858
859         fp2 = fopen(file2, "r");
860
861         if (fp2 == NULL) {
862                 ret = DRMTEST_ERR_IO;
863                 goto error;
864         }
865
866         ch1 = getc(fp1);
867         ch2 = getc(fp2);
868
869         while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2)) {
870                 ch1 = getc(fp1);
871                 ch2 = getc(fp2);
872         }
873
874         if (ch1 == ch2)
875                 *identical = DRMTEST_IDENTICAL;
876         else
877                 *identical = DRMTEST_NOTIDENTICAL;
878
879 error:
880
881         if (fp1 != NULL)
882                 fclose(fp1);
883
884         if (fp2 != NULL)
885                 fclose(fp2);
886
887         return ret;
888 }
889