forked from nx111/oscam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-emulator-osemu.c
6776 lines (5798 loc) · 164 KB
/
module-emulator-osemu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define MODULE_LOG_PREFIX "emu"
#include "globals.h"
#include "ffdecsa/ffdecsa.h"
#include "cscrypt/bn.h"
#include "cscrypt/des.h"
#include "cscrypt/idea.h"
#include "cscrypt/md5.h"
#ifdef WITH_EMU
#include "oscam-aes.h"
#include "oscam-string.h"
#include "oscam-config.h"
#include "oscam-conf-chk.h"
#include "oscam-time.h"
#include "module-newcamd-des.h"
#include "reader-dre-common.h"
// from reader-viaccess.c:
void hdSurEncPhase1_D2_0F_11(uint8_t *CWs);
void hdSurEncPhase2_D2_0F_11(uint8_t *CWs);
void hdSurEncPhase1_D2_13_15(uint8_t *cws);
void hdSurEncPhase2_D2_13_15(uint8_t *cws);
#else
#include "cscrypt/viades.h"
#include "via3surenc.h"
#include "dre2overcrypt.h"
#endif
#include "module-emulator-osemu.h"
#include "module-emulator-stream.h"
// Version info
uint32_t GetOSemuVersion(void)
{
return atoi("$Version: 771 $"+10);
}
/*
* Key DB
*
* The Emu reader gets keys from the OSCcam-Emu binary and the "SoftCam.Key" file.
*
* The keys are stored in structures of type "KeyDataContainer", one per CAS. Each
* container points to a dynamically allocated array of type "KeyData", which holds
* the actual keys. The array initially holds up to 64 keys (64 * KeyData), and it
* is expanded by 16 every time it's filled with keys. The "KeyDataContainer" also
* includes info about the number of keys it contains ("KeyCount") and the maximum
* number of keys it can store ("KeyMax").
*
* The "KeyData" structure on the other hand, stores the actual key information,
* including the "identifier", "provider", "keyName", "key" and "keyLength". There
* is also a "nextKey" pointer to a similar "KeyData" structure which is only used
* for Irdeto multiple keys, in a linked list style structure. For all other CAS,
* the "nextKey" is a "NULL" pointer.
*
* For storing keys, the "SetKey" function is used. Duplicate keys are not allowed.
* When storing a key that is already present in the database, its "key" value is
* updated with the new one. For reading keys from the database, the "FindKey"
* function is used. To delete all keys in a container, the "DeleteKeysInContainer"
* function can be called.
*/
static char *emu_keyfile_path = NULL;
void set_emu_keyfile_path(const char *path)
{
if(emu_keyfile_path != NULL) {
free(emu_keyfile_path);
}
emu_keyfile_path = (char*)malloc(strlen(path)+1);
if(emu_keyfile_path == NULL) {
return;
}
memcpy(emu_keyfile_path, path, strlen(path));
emu_keyfile_path[strlen(path)] = 0;
}
int32_t CharToBin(uint8_t *out, const char *in, uint32_t inLen)
{
uint32_t i, tmp;
for(i=0; i<inLen/2; i++) {
if(sscanf(in + i*2, "%02X", &tmp) != 1) {
return 0;
}
out[i] = (uint8_t)tmp;
}
return 1;
}
KeyDataContainer CwKeys = { NULL, 0, 0 };
KeyDataContainer ViKeys = { NULL, 0, 0 };
KeyDataContainer NagraKeys = { NULL, 0, 0 };
KeyDataContainer IrdetoKeys = { NULL, 0, 0 };
KeyDataContainer NDSKeys = { NULL, 0, 0 };
KeyDataContainer BissKeys = { NULL, 0, 0 };
KeyDataContainer PowervuKeys = { NULL, 0, 0 };
KeyDataContainer DreKeys = { NULL, 0, 0 };
KeyDataContainer TandbergKeys = { NULL, 0, 0 };
static KeyDataContainer *GetKeyContainer(char identifier)
{
switch(identifier) {
case 'W':
return &CwKeys;
case 'V':
return &ViKeys;
case 'N':
return &NagraKeys;
case 'I':
return &IrdetoKeys;
case 'S':
return &NDSKeys;
case 'F':
return &BissKeys;
case 'P':
return &PowervuKeys;
case 'D':
return &DreKeys;
case 'T':
return &TandbergKeys;
default:
return NULL;
}
}
static void Date2Str(char *dateStr, uint8_t len, int8_t offset, uint8_t format)
{
// Creates a formatted date string for use in various functions.
// A positive or negative time offset (in hours) can be set as well
// as the format of the output string.
time_t rawtime;
struct tm timeinfo;
time(&rawtime);
rawtime += (time_t) offset * 60 * 60; // Add a positive or negative offset
localtime_r(&rawtime, &timeinfo);
switch (format)
{
case 1: // Use in WriteKeyToFile()
strftime(dateStr, len, "%c", &timeinfo);
break;
case 2: // Used in BissAnnotate()
strftime(dateStr, len, "%F @ %R", &timeinfo);
break;
case 3: // Used in SetKey(), BissAnnotate()
strftime(dateStr, len, "%y%m%d%H", &timeinfo);
break;
}
}
static void WriteKeyToFile(char identifier, uint32_t provider, const char *keyName, uint8_t *key, uint32_t keyLength, char* comment)
{
char line[1200], dateText[100];
uint32_t pathLength;
struct dirent *pDirent;
DIR *pDir;
char *path, *filepath, filename[EMU_KEY_FILENAME_MAX_LEN+1], *keyValue;
FILE *file = NULL;
uint8_t fileNameLen = strlen(EMU_KEY_FILENAME);
pathLength = strlen(emu_keyfile_path);
path = (char*)malloc(pathLength+1);
if(path == NULL) {
return;
}
strncpy(path, emu_keyfile_path, pathLength+1);
pathLength = strlen(path);
if(pathLength >= fileNameLen && strcasecmp(path+pathLength-fileNameLen, EMU_KEY_FILENAME) == 0) {
// cut file name
path[pathLength-fileNameLen] = '\0';
}
pathLength = strlen(path);
if(path[pathLength-1] == '/' || path[pathLength-1] == '\\') {
// cut trailing /
path[pathLength-1] = '\0';
}
pDir = opendir(path);
if (pDir == NULL) {
cs_log("Cannot open key file path: %s", path);
free(path);
return;
}
while((pDirent = readdir(pDir)) != NULL) {
if(strcasecmp(pDirent->d_name, EMU_KEY_FILENAME) == 0) {
strncpy(filename, pDirent->d_name, sizeof(filename));
break;
}
}
closedir(pDir);
if(pDirent == NULL) {
strncpy(filename, EMU_KEY_FILENAME, sizeof(filename));
}
pathLength = strlen(path)+1+strlen(filename)+1;
filepath = (char*)malloc(pathLength);
if(filepath == NULL) {
free(path);
return;
}
snprintf(filepath, pathLength, "%s/%s", path, filename);
free(path);
cs_log("Writing key file: %s", filepath);
file = fopen(filepath, "a");
free(filepath);
if(file == NULL) {
return;
}
Date2Str(dateText, sizeof(dateText), 0, 1);
keyValue = (char*)malloc((keyLength*2)+1);
if(keyValue == NULL) {
fclose(file);
return;
}
cs_hexdump(0, key, keyLength, keyValue, (keyLength*2)+1);
if(comment)
{
snprintf(line, sizeof(line), "\n%c %.4X %s %s ; added by OSEmu %s %s\n", identifier, provider, keyName, keyValue, dateText, comment);
}
else
{
snprintf(line, sizeof(line), "\n%c %.4X %s %s ; added by OSEmu %s\n", identifier, provider, keyName, keyValue, dateText);
}
cs_log("Key written: %c %.4X %s %s", identifier, provider, keyName, keyValue);
free(keyValue);
fwrite(line, strlen(line), 1, file);
fclose(file);
}
int32_t SetKey(char identifier, uint32_t provider, char *keyName, uint8_t *orgKey, uint32_t keyLength,
uint8_t writeKey, char *comment, struct s_reader *rdr)
{
uint32_t i, j;
uint8_t *tmpKey = NULL;
KeyDataContainer *KeyDB;
KeyData *tmpKeyData, *newKeyData;
identifier = (char)toupper((int)identifier);
KeyDB = GetKeyContainer(identifier);
if(KeyDB == NULL) {
return 0;
}
keyName = strtoupper(keyName);
if (identifier == 'F') // Prepare BISS keys before saving to the db
{
// Convert legacy BISS "00" & "01" keynames
if (0 == strcmp(keyName, "00") || 0 == strcmp(keyName, "01"))
{
keyName = "00000000";
}
// All keyNames should have a length of 8 after converting
if (strlen(keyName) != 8)
{
cs_log("WARNING: Wrong key format in %s: F %08X %s", EMU_KEY_FILENAME, provider, keyName);
return 0;
}
// Verify date-coded keyName (if enabled), ignoring old (expired) keys
if (rdr->emu_datecodedenabled)
{
char timeStr[9];
Date2Str(timeStr, sizeof(timeStr), 0, 3);
// Reject old date-coded keys, but allow our "00000000" evergreen label
if (strcmp("00000000", keyName) != 0 && strcmp(timeStr, keyName) >= 0)
{
return 0;
}
}
}
// fix checksum for BISS keys with a length of 6
if (identifier == 'F' && keyLength == 6)
{
tmpKey = (uint8_t*)malloc(8*sizeof(uint8_t));
if(tmpKey == NULL) {
return 0;
}
tmpKey[0] = orgKey[0];
tmpKey[1] = orgKey[1];
tmpKey[2] = orgKey[2];
tmpKey[3] = ((orgKey[0] + orgKey[1] + orgKey[2]) & 0xff);
tmpKey[4] = orgKey[3];
tmpKey[5] = orgKey[4];
tmpKey[6] = orgKey[5];
tmpKey[7] = ((orgKey[3] + orgKey[4] + orgKey[5]) & 0xff);
keyLength = 8;
}
else // All keys with a length of 8, including BISS
{
tmpKey = (uint8_t*)malloc(keyLength*sizeof(uint8_t));
if(tmpKey == NULL) {
return 0;
}
memcpy(tmpKey, orgKey, keyLength);
}
// fix patched mgcamd format for Irdeto
if(identifier == 'I' && provider < 0xFFFF) {
provider = provider<<8;
}
// key already exists on db, update its value
for(i=0; i<KeyDB->keyCount; i++) {
if(KeyDB->EmuKeys[i].provider != provider) {
continue;
}
// Don't match keyName (i.e. expiration date) for BISS
if(identifier != 'F' && strcmp(KeyDB->EmuKeys[i].keyName, keyName)) {
continue;
}
// allow multiple keys for Irdeto
if(identifier == 'I')
{
// reject duplicates
tmpKeyData = &KeyDB->EmuKeys[i];
do {
if(memcmp(tmpKeyData->key, tmpKey, tmpKeyData->keyLength < keyLength ? tmpKeyData->keyLength : keyLength) == 0) {
free(tmpKey);
return 0;
}
tmpKeyData = tmpKeyData->nextKey;
}
while(tmpKeyData != NULL);
// add new key
newKeyData = (KeyData*)malloc(sizeof(KeyData));
if(newKeyData == NULL) {
free(tmpKey);
return 0;
}
newKeyData->identifier = identifier;
newKeyData->provider = provider;
if(strlen(keyName) < EMU_MAX_CHAR_KEYNAME) {
strncpy(newKeyData->keyName, keyName, EMU_MAX_CHAR_KEYNAME);
}
else {
memcpy(newKeyData->keyName, keyName, EMU_MAX_CHAR_KEYNAME);
}
newKeyData->keyName[EMU_MAX_CHAR_KEYNAME-1] = 0;
newKeyData->key = tmpKey;
newKeyData->keyLength = keyLength;
newKeyData->nextKey = NULL;
tmpKeyData = &KeyDB->EmuKeys[i];
j = 0;
while(tmpKeyData->nextKey != NULL) {
if(j == 0xFE)
{
break;
}
tmpKeyData = tmpKeyData->nextKey;
j++;
}
if(tmpKeyData->nextKey)
{
NULLFREE(tmpKeyData->nextKey->key);
NULLFREE(tmpKeyData->nextKey);
}
tmpKeyData->nextKey = newKeyData;
if(writeKey) {
WriteKeyToFile(identifier, provider, keyName, tmpKey, keyLength, comment);
}
}
else // identifier != 'I'
{
free(KeyDB->EmuKeys[i].key);
KeyDB->EmuKeys[i].key = tmpKey;
KeyDB->EmuKeys[i].keyLength = keyLength;
if (identifier == 'F') // Update keyName (i.e. expiration date) for BISS
{
strncpy(KeyDB->EmuKeys[i].keyName, keyName, EMU_MAX_CHAR_KEYNAME);
}
if(writeKey) {
WriteKeyToFile(identifier, provider, keyName, tmpKey, keyLength, comment);
}
}
return 1;
}
// key does not exist on db
if(KeyDB->keyCount+1 > KeyDB->keyMax)
{
if(KeyDB->EmuKeys == NULL) // db is empty
{
KeyDB->EmuKeys = (KeyData*)malloc(sizeof(KeyData)*(KeyDB->keyMax+64));
if(KeyDB->EmuKeys == NULL) {
free(tmpKey);
return 0;
}
KeyDB->keyMax+=64;
}
else // db is full, expand it
{
tmpKeyData = (KeyData*)realloc(KeyDB->EmuKeys, sizeof(KeyData)*(KeyDB->keyMax+16));
if(tmpKeyData == NULL) {
free(tmpKey);
return 0;
}
KeyDB->EmuKeys = tmpKeyData;
KeyDB->keyMax+=16;
}
}
KeyDB->EmuKeys[KeyDB->keyCount].identifier = identifier;
KeyDB->EmuKeys[KeyDB->keyCount].provider = provider;
if(strlen(keyName) < EMU_MAX_CHAR_KEYNAME) {
strncpy(KeyDB->EmuKeys[KeyDB->keyCount].keyName, keyName, EMU_MAX_CHAR_KEYNAME);
}
else {
memcpy(KeyDB->EmuKeys[KeyDB->keyCount].keyName, keyName, EMU_MAX_CHAR_KEYNAME);
}
KeyDB->EmuKeys[KeyDB->keyCount].keyName[EMU_MAX_CHAR_KEYNAME-1] = 0;
KeyDB->EmuKeys[KeyDB->keyCount].key = tmpKey;
KeyDB->EmuKeys[KeyDB->keyCount].keyLength = keyLength;
KeyDB->EmuKeys[KeyDB->keyCount].nextKey = NULL;
KeyDB->keyCount++;
if(writeKey) {
WriteKeyToFile(identifier, provider, keyName, tmpKey, keyLength, comment);
}
return 1;
}
int32_t FindKey(char identifier, uint32_t provider, uint32_t providerIgnoreMask, char *keyName, uint8_t *key,
uint32_t maxKeyLength, uint8_t isCriticalKey, uint32_t keyRef, uint8_t matchLength, uint32_t *getProvider)
{
uint32_t i;
uint16_t j;
uint8_t provider_matching_key_count = 0;
KeyDataContainer *KeyDB;
KeyData *tmpKeyData;
KeyDB = GetKeyContainer(identifier);
if(KeyDB == NULL) {
return 0;
}
for(i=0; i<KeyDB->keyCount; i++) {
if((KeyDB->EmuKeys[i].provider & ~providerIgnoreMask) != provider) {
continue;
}
// Don't match keyName (i.e. expiration date) for BISS
if(identifier != 'F' && strcmp(KeyDB->EmuKeys[i].keyName, keyName)) {
continue;
}
//matchLength cannot be used when multiple keys are allowed
//for a single provider/keyName combination.
//Currently this is only the case for Irdeto keys.
if(matchLength && KeyDB->EmuKeys[i].keyLength != maxKeyLength) {
continue;
}
if(providerIgnoreMask) {
if(provider_matching_key_count < keyRef) {
provider_matching_key_count++;
continue;
}
else {
keyRef = 0;
}
}
tmpKeyData = &KeyDB->EmuKeys[i];
j = 0;
while(j<keyRef && tmpKeyData->nextKey != NULL) {
j++;
tmpKeyData = tmpKeyData->nextKey;
}
if(j == keyRef) {
memcpy(key, tmpKeyData->key, tmpKeyData->keyLength > maxKeyLength ? maxKeyLength : tmpKeyData->keyLength);
if(tmpKeyData->keyLength < maxKeyLength) {
memset(key+tmpKeyData->keyLength, 0, maxKeyLength - tmpKeyData->keyLength);
}
if (identifier == 'F') // Report the keyName of found key back to BissGetKey()
{
strncpy(keyName, tmpKeyData->keyName, EMU_MAX_CHAR_KEYNAME);
}
if(getProvider != NULL) {
(*getProvider) = tmpKeyData->provider;
}
return 1;
}
else {
break;
}
}
if (isCriticalKey)
{
cs_log("Key not found: %c %X %s", identifier, provider, keyName);
}
return 0;
}
static int32_t UpdateKey(char identifier, uint32_t provider, char *keyName, uint8_t *key, uint32_t keyLength, uint8_t writeKey, char *comment)
{
uint32_t keyRef = 0;
uint8_t *tmpKey = (uint8_t*)malloc(sizeof(uint8_t)*keyLength);
if(tmpKey == NULL)
{
return 0;
}
while(FindKey(identifier, provider, 0, keyName, tmpKey, keyLength, 0, keyRef, 0, NULL))
{
if(memcmp(tmpKey, key, keyLength) == 0)
{
free(tmpKey);
return 0;
}
keyRef++;
}
free(tmpKey);
return SetKey(identifier, provider, keyName, key, keyLength, writeKey, comment, NULL);
}
static int32_t UpdateKeysByProviderMask(char identifier, uint32_t provider, uint32_t providerIgnoreMask, char *keyName, uint8_t *key,
uint32_t keyLength, char *comment)
{
int32_t ret = 0;
uint32_t foundProvider = 0;
uint32_t keyRef = 0;
uint8_t *tmpKey = (uint8_t*)malloc(sizeof(uint8_t)*keyLength);
if(tmpKey == NULL)
{
return 0;
}
while(FindKey(identifier, (provider & ~providerIgnoreMask), providerIgnoreMask, keyName, tmpKey, keyLength, 0, keyRef, 0, &foundProvider))
{
keyRef++;
if(memcmp(tmpKey, key, keyLength) == 0)
{
continue;
}
if(SetKey(identifier, foundProvider, keyName, key, keyLength, 1, comment, NULL))
{
ret = 1;
}
}
free(tmpKey);
return ret;
}
int32_t DeleteKeysInContainer(char identifier)
{
// Deletes all keys stored in memory for the specified identifier,
// but keeps the container itself, re-initialized at { NULL, 0, 0 }.
// Returns the count of deleted keys.
uint32_t oldKeyCount, i;
KeyData *tmpKeyData;
KeyDataContainer *KeyDB = GetKeyContainer(identifier);
if (KeyDB == NULL || KeyDB->EmuKeys == NULL || KeyDB->keyCount == 0)
{
return 0;
}
for (i = 0; i < KeyDB->keyCount; i++)
{
// For Irdeto multiple keys only (linked list structure)
while (KeyDB->EmuKeys[i].nextKey != NULL)
{
tmpKeyData = KeyDB->EmuKeys[i].nextKey;
KeyDB->EmuKeys[i].nextKey = KeyDB->EmuKeys[i].nextKey->nextKey;
free(tmpKeyData->key); // Free key
free(tmpKeyData); // Free KeyData
}
// For single keys (all identifiers, including Irdeto)
free(KeyDB->EmuKeys[i].key); // Free key
}
// Free the KeyData array
NULLFREE(KeyDB->EmuKeys);
oldKeyCount = KeyDB->keyCount;
KeyDB->keyCount = 0;
KeyDB->keyMax = 0;
return oldKeyCount;
}
void clear_emu_keydata(void)
{
uint32_t total = 0;
total = CwKeys.keyCount;
total += ViKeys.keyCount;
total += NagraKeys.keyCount;
total += IrdetoKeys.keyCount;
total += NDSKeys.keyCount;
total += BissKeys.keyCount;
total += PowervuKeys.keyCount;
total += DreKeys.keyCount;
total += TandbergKeys.keyCount;
if (total != 0)
{
cs_log("Freeing keys in memory: W:%d V:%d N:%d I:%d S:%d F:%d P:%d D:%d T:%d", \
CwKeys.keyCount, ViKeys.keyCount, NagraKeys.keyCount, \
IrdetoKeys.keyCount, NDSKeys.keyCount, BissKeys.keyCount, \
PowervuKeys.keyCount, DreKeys.keyCount, TandbergKeys.keyCount);
DeleteKeysInContainer('W');
DeleteKeysInContainer('V');
DeleteKeysInContainer('N');
DeleteKeysInContainer('I');
DeleteKeysInContainer('S');
DeleteKeysInContainer('F');
DeleteKeysInContainer('P');
DeleteKeysInContainer('D');
DeleteKeysInContainer('T');
}
}
uint8_t read_emu_keyfile(struct s_reader *rdr, const char *opath)
{
char line[1200], keyName[EMU_MAX_CHAR_KEYNAME], keyString[1026];
uint32_t pathLength, provider, keyLength;
uint8_t *key;
struct dirent *pDirent;
DIR *pDir;
char *path, *filepath, filename[EMU_KEY_FILENAME_MAX_LEN+1];
FILE *file = NULL;
char identifier;
uint8_t fileNameLen = strlen(EMU_KEY_FILENAME);
pathLength = strlen(opath);
path = (char*)malloc(pathLength+1);
if(path == NULL) {
return 0;
}
strncpy(path, opath, pathLength+1);
pathLength = strlen(path);
if(pathLength >= fileNameLen && strcasecmp(path+pathLength-fileNameLen, EMU_KEY_FILENAME) == 0) {
// cut file name
path[pathLength-fileNameLen] = '\0';
}
pathLength = strlen(path);
if(path[pathLength-1] == '/' || path[pathLength-1] == '\\') {
// cut trailing /
path[pathLength-1] = '\0';
}
pDir = opendir(path);
if (pDir == NULL) {
cs_log("Cannot open key file path: %s", path);
free(path);
return 0;
}
while((pDirent = readdir(pDir)) != NULL) {
if(strcasecmp(pDirent->d_name, EMU_KEY_FILENAME) == 0) {
strncpy(filename, pDirent->d_name, sizeof(filename));
break;
}
}
closedir(pDir);
if(pDirent == NULL) {
cs_log("Key file not found in: %s", path);
free(path);
return 0;
}
pathLength = strlen(path)+1+strlen(filename)+1;
filepath = (char*)malloc(pathLength);
if(filepath == NULL) {
free(path);
return 0;
}
snprintf(filepath, pathLength, "%s/%s", path, filename);
free(path);
cs_log("Reading key file: %s", filepath);
file = fopen(filepath, "r");
free(filepath);
if(file == NULL) {
return 0;
}
set_emu_keyfile_path(opath);
while(fgets(line, 1200, file)) {
if(sscanf(line, "%c %8x %11s %1024s", &identifier, &provider, keyName, keyString) != 4) {
continue;
}
keyLength = strlen(keyString)/2;
key = (uint8_t*)malloc(keyLength);
if(key == NULL) {
fclose(file);
return 0;
}
if (CharToBin(key, keyString, strlen(keyString))) // Conversion OK
{
SetKey(identifier, provider, keyName, key, keyLength, 0, NULL, rdr);
}
else // Non-hex characters in keyString
{
if ((identifier != ';' && identifier != '#' && // Skip warning for comments, etc.
identifier != '=' && identifier != '-' &&
identifier != ' ') &&
!(identifier == 'F' && 0 == strncmp(keyString, "XXXXXXXXXXXX", 12))) // Skip warning for BISS 'Example key' lines
{
// Alert user regarding faulty line
cs_log("WARNING: non-hex value in %s at %c %04X %s %s", EMU_KEY_FILENAME, identifier, provider, keyName, keyString);
}
}
free(key);
}
fclose(file);
return 1;
}
#if !defined(__APPLE__) && !defined(__ANDROID__)
extern uint8_t SoftCamKey_Data[] __asm__("_binary_SoftCam_Key_start");
extern uint8_t SoftCamKey_DataEnd[] __asm__("_binary_SoftCam_Key_end");
void read_emu_keymemory(struct s_reader *rdr)
{
char *keyData, *line, *saveptr, keyName[EMU_MAX_CHAR_KEYNAME], keyString[1026];
uint32_t provider, keyLength;
uint8_t *key;
char identifier;
keyData = (char*)malloc(SoftCamKey_DataEnd-SoftCamKey_Data+1);
if(keyData == NULL) {
return;
}
memcpy(keyData, SoftCamKey_Data, SoftCamKey_DataEnd-SoftCamKey_Data);
keyData[SoftCamKey_DataEnd-SoftCamKey_Data] = 0x00;
line = strtok_r(keyData, "\n", &saveptr);
while(line != NULL) {
if(sscanf(line, "%c %8x %11s %1024s", &identifier, &provider, keyName, keyString) != 4) {
line = strtok_r(NULL, "\n", &saveptr);
continue;
}
keyLength = strlen(keyString)/2;
key = (uint8_t*)malloc(keyLength);
if(key == NULL) {
free(keyData);
return;
}
if (CharToBin(key, keyString, strlen(keyString))) // Conversion OK
{
SetKey(identifier, provider, keyName, key, keyLength, 0, NULL, rdr);
}
else // Non-hex characters in keyString
{
if ((identifier != ';' && identifier != '#' && // Skip warning for comments, etc.
identifier != '=' && identifier != '-' &&
identifier != ' ') &&
!(identifier == 'F' && 0 == strncmp(keyString, "XXXXXXXXXXXX", 12))) // Skip warning for BISS 'Example key' lines
{
// Alert user regarding faulty line
cs_log("WARNING: non-hex value in internal keyfile at %c %04X %s %s", identifier, provider, keyName, keyString);
}
}
free(key);
line = strtok_r(NULL, "\n", &saveptr);
}
free(keyData);
}
#endif
void read_emu_eebin(const char *path, const char *name)
{
char tmp[256];
FILE *file = NULL;
uint8_t i, buffer[64][32], dummy[2][32];
uint32_t prvid;
// Set path
if (path != NULL)
{
snprintf(tmp, 256, "%s%s", path, name);
}
else // No path set, use SoftCam.Keys's path
{
snprintf(tmp, 256, "%s%s", emu_keyfile_path, name);
}
// Read file to buffer
if ((file = fopen(tmp, "rb")) != NULL)
{
cs_log("Reading key file: %s", tmp);
if (fread(buffer, 1, sizeof(buffer), file) != sizeof(buffer))
{
cs_log("Corrupt key file: %s", tmp);
fclose(file);
return;
}
fclose(file);
}
else
{
if (path != NULL)
{
cs_log("Cannot open key file: %s", tmp);
}
return;
}
// Save keys to db
memset(dummy[0], 0x00, 32);
memset(dummy[1], 0xFF, 32);
prvid = (strncmp(name, "ee36.bin", 9) == 0) ? 0x4AE111 : 0x4AE114;
for (i = 0; i < 32; i++) // Set "3B" type keys
{
// Write keys if they have "real" values
if ((memcmp(buffer[i], dummy[0], 32) !=0) && (memcmp(buffer[i], dummy[1], 32) != 0))
{
snprintf(tmp, 5, "3B%02X", i);
SetKey('D', prvid, tmp, buffer[i], 32, 0, NULL, NULL);
}
}
for (i = 0; i < 32; i++) // Set "56" type keys
{
// Write keys if they have "real" values
if ((memcmp(buffer[32 + i], dummy[0], 32) !=0) && (memcmp(buffer[32 + i], dummy[1], 32) != 0))
{
snprintf(tmp, 5, "56%02X", i);
SetKey('D', prvid, tmp, buffer[32 + i], 32, 0, NULL, NULL);
}
}
}
void read_emu_deskey(uint8_t *dreOverKey, uint8_t len)
{
uint8_t i;
if (len == 128)
{
cs_log("Reading DreCrypt overcrypt (ADEC) key");
for (i = 0; i < 16; i++)
{
SetKey('D', i, "OVER", dreOverKey + (i * 8), 8, 0, NULL, NULL);
}
}
else if ((len != 0 && len < 128) || len > 128)
{
cs_log("DreCrypt overcrypt (ADEC) key has wrong length");
}
}
// Shared functions
static inline uint16_t GetEcmLen(const uint8_t *ecm)
{
return (((ecm[1] & 0x0f)<< 8) | ecm[2]) +3;
}
static void ReverseMem(uint8_t *in, int32_t len)
{
uint8_t temp;
int32_t i;
for(i = 0; i < (len / 2); i++) {
temp = in[i];
in[i] = in[len - i - 1];
in[len - i - 1] = temp;
}
}
static void ReverseMemInOut(uint8_t *out, const uint8_t *in, int32_t n)
{
if(n>0) {
out+=n;
do {
*(--out)=*(in++);
}
while(--n);
}
}
static int8_t EmuRSAInput(BIGNUM *d, const uint8_t *in, int32_t n, int8_t le)
{
int8_t result = 0;
if(le) {
uint8_t *tmp = (uint8_t *)malloc(sizeof(uint8_t)*n);
if(tmp == NULL) {
return 0;
}
ReverseMemInOut(tmp,in,n);
result = BN_bin2bn(tmp,n,d)!=0;
free(tmp);
}
else {
result = BN_bin2bn(in,n,d)!=0;
}
return result;
}
static int32_t EmuRSAOutput(uint8_t *out, int32_t n, BIGNUM *r, int8_t le)
{
int32_t s = BN_num_bytes(r);
if(s>n) {
uint8_t *buff = (uint8_t *)malloc(sizeof(uint8_t)*s);
if(buff == NULL) {
return 0;
}
BN_bn2bin(r,buff);
memcpy(out,buff+s-n,n);
free(buff);
}
else if(s<n) {
int32_t l=n-s;
memset(out,0,l);
BN_bn2bin(r,out+l);
}
else {
BN_bn2bin(r,out);
}
if(le) {
ReverseMem(out,n);
}
return s;
}
static int32_t EmuRSA(uint8_t *out, const uint8_t *in, int32_t n, BIGNUM *exp, BIGNUM *mod, int8_t le)
{
BN_CTX *ctx;
BIGNUM *r, *d;
int32_t result = 0;
ctx = BN_CTX_new();
r = BN_new();
d = BN_new();
if(EmuRSAInput(d,in,n,le) && BN_mod_exp(r,d,exp,mod,ctx)) {
result = EmuRSAOutput(out,n,r,le);
}
BN_free(d);
BN_free(r);
BN_CTX_free(ctx);
return result;
}
static inline void xxor(uint8_t *data, int32_t len, const uint8_t *v1, const uint8_t *v2)
{