-
Notifications
You must be signed in to change notification settings - Fork 90
/
oscam-emu.patch
10935 lines (10876 loc) · 274 KB
/
oscam-emu.patch
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
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt (revision 11757)
+++ CMakeLists.txt (working copy)
@@ -454,6 +454,22 @@
# Manage config.h based on command line parameters
# Manipulate config file based on given parameters and read unset parameters
+execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled WITH_EMU OUTPUT_VARIABLE CONFIG_WITH_EMU OUTPUT_STRIP_TRAILING_WHITESPACE)
+if (CONFIG_WITH_EMU MATCHES "Y" AND NOT WITH_EMU EQUAL 1)
+ add_definitions ("-DWITH_EMU")
+ set (WITH_EMU "1")
+ message (STATUS " EMU is added by config compiling with EMU")
+endif (CONFIG_WITH_EMU MATCHES "Y" AND NOT WITH_EMU EQUAL 1)
+
+if (WITH_EMU)
+ execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --enabled WITH_SOFTCAM OUTPUT_VARIABLE CONFIG_WITH_SOFTCAM OUTPUT_STRIP_TRAILING_WHITESPACE)
+ if (CONFIG_WITH_SOFTCAM MATCHES "Y" AND NOT WITH_SOFTCAM EQUAL 1)
+ add_definitions ("-DWITH_SOFTCAM")
+ set (WITH_SOFTCAM "1")
+ message (STATUS " SOFTCAM is added by config linking SoftCam.Key")
+ endif (CONFIG_WITH_SOFTCAM MATCHES "Y" AND NOT WITH_SOFTCAM EQUAL 1)
+endif (WITH_EMU)
+
execute_process (COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/config.sh --show-valid OUTPUT_VARIABLE config_vars_string OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REGEX MATCHALL "[A-Z0-9_]+" config_vars ${config_vars_string})
@@ -790,7 +806,25 @@
endif (HAVE_LIBUSB)
endif( HAVE_LIBRT OR HAVE_LIBRT_STATIC)
-#--------------------------------------------------------------------------------
+#-------------------------------------------------------------------------------
+
+if (NOT OSCamOperatingSystem MATCHES "Mac OS X")
+if (NOT DEFINED ENV{ANDROID_NDK})
+if (NOT DEFINED ENV{ANDROID_STANDALONE_TOOLCHAIN})
+ if (WITH_SOFTCAM)
+ if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
+ execute_process (COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key ${CMAKE_CURRENT_BINARY_DIR}/SoftCam.Key)
+ else (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
+ execute_process (COMMAND touch ${CMAKE_CURRENT_BINARY_DIR}/SoftCam.Key)
+ endif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SoftCam.Key)
+ set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--format=binary -Wl,SoftCam.Key -Wl,--format=default")
+ if (NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
+ set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack")
+ endif (NOT OSCamOperatingSystem MATCHES "Windows/Cygwin")
+ endif (WITH_SOFTCAM)
+endif (NOT DEFINED ENV{ANDROID_STANDALONE_TOOLCHAIN})
+endif (NOT DEFINED ENV{ANDROID_NDK})
+endif (NOT OSCamOperatingSystem MATCHES "Mac OS X")
#----------------------- installation -----------------------------
@@ -898,4 +932,11 @@
endif(STATICLIBDVBCSA AND NOT LIBDVBCSADIR)
endif (HAVE_LIBDVBCSA)
+if (WITH_EMU)
+ message (STATUS " Compile with EMU support")
+ if (WITH_SOFTCAM)
+ message (STATUS " SoftCam.Key will be linked as well")
+ endif (WITH_SOFTCAM)
+endif (WITH_EMU)
+
message (STATUS "")
Index: Makefile
===================================================================
--- Makefile (revision 11757)
+++ Makefile (working copy)
@@ -31,6 +31,9 @@
override USE_SSL=1
override USE_LIBCRYPTO=1
endif
+ifeq "$(shell ./config.sh --enabled WITH_EMU)" "Y"
+ override USE_LIBCRYPTO=1
+endif
ifdef USE_SSL
override USE_LIBCRYPTO=1
endif
@@ -298,6 +301,31 @@
SRC-$(CONFIG_MODULE_CCCAM) += module-cccam.c
SRC-$(CONFIG_MODULE_CCCSHARE) += module-cccshare.c
SRC-$(CONFIG_MODULE_CONSTCW) += module-constcw.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-osemu.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-biss.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-cryptoworks.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-director.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-irdeto.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-nagravision.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-powervu.c
+SRC-$(CONFIG_WITH_EMU) += module-emulator-viaccess.c
+ifeq "$(CONFIG_WITH_EMU)" "y"
+ifeq "$(CONFIG_WITH_SOFTCAM)" "y"
+UNAME := $(shell uname -s)
+ifneq ($(UNAME),Darwin)
+ifndef ANDROID_NDK
+ifndef ANDROID_STANDALONE_TOOLCHAIN
+TOUCH_SK := $(shell touch SoftCam.Key)
+override LDFLAGS += -Wl,--format=binary -Wl,SoftCam.Key -Wl,--format=default
+ifneq ($(uname_S),Cygwin)
+override LDFLAGS += -Wl,-z,noexecstack
+endif
+endif
+endif
+endif
+endif
+endif
SRC-$(CONFIG_CS_CACHEEX) += module-csp.c
SRC-$(CONFIG_CW_CYCLE_CHECK) += module-cw-cycle-check.c
SRC-$(CONFIG_WITH_AZBOX) += module-dvbapi-azbox.c
Index: config.h
===================================================================
--- config.h (revision 11757)
+++ config.h (working copy)
@@ -1,6 +1,8 @@
#ifndef CONFIG_H_
#define CONFIG_H_
+#define WITH_EMU 1
+#define WITH_SOFTCAM 1
#define WEBIF 1
#define WEBIF_LIVELOG 1
#define WEBIF_JQUERY 1
Index: config.sh
===================================================================
--- config.sh (revision 11757)
+++ config.sh (working copy)
@@ -1,6 +1,6 @@
#!/bin/sh
-addons="WEBIF WEBIF_LIVELOG WEBIF_JQUERY WITH_SSL HAVE_DVBAPI WITH_EXTENDED_CW WITH_NEUTRINO READ_SDT_CHARSETS CS_ANTICASC WITH_DEBUG MODULE_MONITOR WITH_LB CS_CACHEEX CS_CACHEEX_AIO CW_CYCLE_CHECK LCDSUPPORT LEDSUPPORT CLOCKFIX IPV6SUPPORT WITH_ARM_NEON"
+addons="WEBIF WEBIF_LIVELOG WEBIF_JQUERY WITH_SSL HAVE_DVBAPI WITH_EXTENDED_CW WITH_NEUTRINO READ_SDT_CHARSETS CS_ANTICASC WITH_DEBUG MODULE_MONITOR WITH_LB CS_CACHEEX CS_CACHEEX_AIO CW_CYCLE_CHECK LCDSUPPORT LEDSUPPORT CLOCKFIX IPV6SUPPORT WITH_ARM_NEON WITH_EMU WITH_SOFTCAM"
protocols="MODULE_CAMD33 MODULE_CAMD35 MODULE_CAMD35_TCP MODULE_NEWCAMD MODULE_CCCAM MODULE_CCCSHARE MODULE_GBOX MODULE_RADEGAST MODULE_SCAM MODULE_SERIAL MODULE_CONSTCW MODULE_PANDORA MODULE_GHTTP MODULE_STREAMRELAY"
readers="READER_NAGRA READER_NAGRA_MERLIN READER_IRDETO READER_CONAX READER_CRYPTOWORKS READER_SECA READER_VIACCESS READER_VIDEOGUARD READER_DRE READER_TONGFANG READER_BULCRYPT READER_GRIFFIN READER_DGCRYPT"
card_readers="CARDREADER_PHOENIX CARDREADER_INTERNAL CARDREADER_SC8IN1 CARDREADER_MP35 CARDREADER_SMARGO CARDREADER_DB2COM CARDREADER_STAPI CARDREADER_STAPI5 CARDREADER_STINGER CARDREADER_DRECAS"
@@ -26,6 +26,8 @@
# CONFIG_CLOCKFIX=n
# CONFIG_IPV6SUPPORT=n
# CONFIG_WITH_ARM_NEON=n
+CONFIG_WITH_EMU=y
+CONFIG_WITH_SOFTCAM=y
# CONFIG_MODULE_CAMD33=n
CONFIG_MODULE_CAMD35=y
CONFIG_MODULE_CAMD35_TCP=y
@@ -294,13 +296,16 @@
update_deps() {
# Calculate dependencies
- enabled_any $(get_opts readers) $(get_opts card_readers) && enable_opt WITH_CARDREADER >/dev/null
- disabled_all $(get_opts readers) $(get_opts card_readers) && disable_opt WITH_CARDREADER >/dev/null
+ enabled_any $(get_opts readers) $(get_opts card_readers) WITH_EMU && enable_opt WITH_CARDREADER >/dev/null
+ disabled_all $(get_opts readers) $(get_opts card_readers) WITH_EMU && disable_opt WITH_CARDREADER >/dev/null
disabled WEBIF && disable_opt WEBIF_LIVELOG >/dev/null
disabled WEBIF && disable_opt WEBIF_JQUERY >/dev/null
enabled MODULE_CCCSHARE && enable_opt MODULE_CCCAM >/dev/null
enabled_any CARDREADER_DB2COM CARDREADER_MP35 CARDREADER_SC8IN1 CARDREADER_STINGER && enable_opt CARDREADER_PHOENIX >/dev/null
enabled CS_CACHEEX_AIO && enable_opt CS_CACHEEX >/dev/null
+ enabled WITH_EMU && enable_opt READER_VIACCESS >/dev/null
+ enabled WITH_EMU && enable_opt MODULE_NEWCAMD >/dev/null
+ disabled WITH_EMU && disable_opt WITH_SOFTCAM >/dev/null
}
list_config() {
@@ -350,9 +355,9 @@
not_have_flag USE_LIBCRYPTO && echo "CONFIG_LIB_AES=y" || echo "# CONFIG_LIB_AES=n"
enabled MODULE_CCCAM && echo "CONFIG_LIB_RC6=y" || echo "# CONFIG_LIB_RC6=n"
not_have_flag USE_LIBCRYPTO && enabled MODULE_CCCAM && echo "CONFIG_LIB_SHA1=y" || echo "# CONFIG_LIB_SHA1=n"
- enabled_any READER_DRE MODULE_SCAM READER_VIACCESS READER_NAGRA READER_NAGRA_MERLIN READER_VIDEOGUARD READER_CONAX && echo "CONFIG_LIB_DES=y" || echo "# CONFIG_LIB_DES=n"
- enabled_any MODULE_CCCAM READER_NAGRA READER_NAGRA_MERLIN READER_SECA && echo "CONFIG_LIB_IDEA=y" || echo "# CONFIG_LIB_IDEA=n"
- not_have_flag USE_LIBCRYPTO && enabled_any READER_CONAX READER_CRYPTOWORKS READER_NAGRA READER_NAGRA_MERLIN && echo "CONFIG_LIB_BIGNUM=y" || echo "# CONFIG_LIB_BIGNUM=n"
+ enabled_any READER_DRE MODULE_SCAM READER_VIACCESS READER_NAGRA READER_NAGRA_MERLIN READER_VIDEOGUARD READER_CONAX WITH_EMU && echo "CONFIG_LIB_DES=y" || echo "# CONFIG_LIB_DES=n"
+ enabled_any MODULE_CCCAM READER_NAGRA READER_NAGRA_MERLIN READER_SECA WITH_EMU && echo "CONFIG_LIB_IDEA=y" || echo "# CONFIG_LIB_IDEA=n"
+ not_have_flag USE_LIBCRYPTO && enabled_any READER_CONAX READER_CRYPTOWORKS READER_NAGRA READER_NAGRA_MERLIN WITH_EMU && echo "CONFIG_LIB_BIGNUM=y" || echo "# CONFIG_LIB_BIGNUM=n"
enabled READER_NAGRA_MERLIN && echo "CONFIG_LIB_MDC2=y" || echo "# CONFIG_LIB_MDC2=n"
enabled READER_NAGRA_MERLIN && echo "CONFIG_LIB_FAST_AES=y" || echo "# CONFIG_LIB_FAST_AES=n"
enabled READER_NAGRA_MERLIN && echo "CONFIG_LIB_SHA256=y" || echo "# CONFIG_LIB_SHA256=n"
@@ -469,6 +474,8 @@
CLOCKFIX "Clockfix (disable on old systems!)" $(check_test "CLOCKFIX") \
IPV6SUPPORT "IPv6 support (experimental)" $(check_test "IPV6SUPPORT") \
WITH_ARM_NEON "ARM NEON (SIMD/MPE) support" $(check_test "WITH_ARM_NEON") \
+ WITH_EMU "Emulator support" $(check_test "WITH_EMU") \
+ WITH_SOFTCAM "Built-in SoftCam.Key" $(check_test "WITH_SOFTCAM") \
2> ${tempfile}
opt=${?}
@@ -706,9 +713,19 @@
revision=`(svnversion -n . 2>/dev/null || printf 0) | sed 's/.*://; s/[^0-9]*$//; s/^$/0/'`
if [ "$revision" = "0" ]
then
- which git > /dev/null 2>&1 && revision=`git log -10 --pretty=%B | grep git-svn-id | head -n 1 | sed -n -e 's/^.*trunk@\([0-9]*\) .*$/\1/p'`
+ which git > /dev/null 2>&1 && revision=`git log -25 --pretty=%B | grep git-svn-id | head -n 1 | sed -n -e 's/^.*trunk@\([0-9]*\) .*$/\1/p'`
fi
- echo $revision
+ emuversion=`grep EMU_VERSION module-emulator-osemu.h | awk '{ print $3 }'`
+ if [ -z "$emuversion" ]
+ then
+ echo "$revision"
+ else
+ echo "$revision-$emuversion"
+ fi
+ break
+ ;;
+ '--emu-version')
+ grep EMU_VERSION module-emulator-osemu.h | awk '{ print $3 }'
break
;;
'-O'|'--detect-osx-sdk-version')
Index: globals.h
===================================================================
--- globals.h (revision 11757)
+++ globals.h (working copy)
@@ -401,7 +401,7 @@
#define CS_ECM_RINGBUFFER_MAX 0x10 // max size for ECM last responsetimes ringbuffer. Keep this set to power of 2 values!
// Support for multiple CWs per channel and other encryption algos
-//#define WITH_EXTENDED_CW 1
+#define WITH_EXTENDED_CW 1
#if defined(READER_DRE) || defined(READER_DRECAS) || defined(READER_VIACCESS) || defined(WITH_EMU)
#define MAX_ECM_SIZE 1024
@@ -1945,6 +1945,7 @@
#ifdef WITH_EMU
FTAB emu_auproviders; // AU providers for Emu reader
int8_t emu_datecodedenabled; // date-coded keys for BISS
+ LLIST *ll_biss2_rsa_keys; // BISS2 RSA keys - Read from external PEM files
#endif
uint8_t cnxlastecm; // == 0 - last ecm has not been paired ecm, > 0 last ecm has been paired ecm
LLIST *emmstat; // emm stats
@@ -2470,6 +2471,10 @@
#define DEFAULT_STREAM_SOURCE_PORT 8001 //Enigma2
#endif
#endif
+#ifdef WITH_EMU
+ uint32_t emu_stream_ecm_delay;
+ int8_t emu_stream_emm_enabled;
+#endif
int32_t max_cache_time; // seconds ecms are stored in ecmcwcache
int32_t max_hitcache_time; // seconds hits are stored in cspec_hitcache (to detect dyn wait_time)
Index: module-dvbapi.c
===================================================================
--- module-dvbapi.c (revision 11757)
+++ module-dvbapi.c (working copy)
@@ -2657,6 +2657,8 @@
er->vpid = demux[demux_id].ECMpids[pid].VPID;
er->pmtpid = demux[demux_id].pmtpid;
er->onid = demux[demux_id].onid;
+ er->tsid = demux[demux_id].tsid;
+ er->ens = demux[demux_id].ens;
er->msgid = msgid;
#ifdef WITH_STAPI5
@@ -2704,19 +2706,30 @@
if(caid_is_fake(demux[demux_id].ECMpids[pid].CAID) || caid_is_biss_fixed(demux[demux_id].ECMpids[pid].CAID))
{
int32_t j, n;
- er->ecmlen = 5;
+ er->ecmlen = 7;
er->ecm[0] = 0x80; // to pass the cache check it must be 0x80 or 0x81
er->ecm[1] = 0x00;
- er->ecm[2] = 0x02;
+ er->ecm[2] = 0x04;
i2b_buf(2, er->srvid, er->ecm + 3);
+ i2b_buf(2, er->pmtpid, er->ecm + 5);
- for(j = 0, n = 5; j < demux[demux_id].STREAMpidcount; j++, n += 2)
+ for(j = 0, n = 7; j < demux[demux_id].STREAMpidcount; j++, n += 2)
{
i2b_buf(2, demux[demux_id].STREAMpids[j], er->ecm + n);
er->ecm[2] += 2;
er->ecmlen += 2;
}
+ er->ens &= 0x0FFFFFFF; // clear top 4 bits (in case of DVB-T/C or garbage), prepare for flagging
+ er->ens |= 0xA0000000; // flag to emu: this is the namespace, not a pid
+
+ i2b_buf(2, er->tsid, er->ecm + er->ecmlen); // place tsid after the last stream pid
+ i2b_buf(2, er->onid, er->ecm + er->ecmlen + 2); // place onid right after tsid
+ i2b_buf(4, er->ens, er->ecm + er->ecmlen + 4); // place namespace at the end of the ecm
+
+ er->ecm[2] += 8;
+ er->ecmlen += 8;
+
cs_log("Demuxer %d trying to descramble PID %d CAID %04X PROVID %06X ECMPID %04X ANY CHID PMTPID %04X VPID %04X",
demux_id,
pid,
Index: module-emulator-biss.c
===================================================================
--- module-emulator-biss.c (nonexistent)
+++ module-emulator-biss.c (working copy)
@@ -0,0 +1,883 @@
+#define MODULE_LOG_PREFIX "emu"
+
+#include "globals.h"
+
+#ifdef WITH_EMU
+
+#include "module-emulator-osemu.h"
+#include "module-emulator-biss.h"
+#include "oscam-aes.h"
+#include "oscam-string.h"
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+//#include <openssl/rsa.h>
+#include <openssl/x509.h>
+
+// DVB-CISSA v1 IV as defined in ETSI TS 103 127
+static const uint8_t dvb_cissa_iv[16] =
+{
+ 0x44, 0x56, 0x42, 0x54, 0x4D, 0x43, 0x50, 0x54,
+ 0x41, 0x45, 0x53, 0x43, 0x49, 0x53, 0x53, 0x41
+};
+
+static void unify_orbitals(uint32_t *namespace)
+{
+ // Unify orbitals to produce same namespace among users
+ // Set positions according to http://satellites-xml.org
+
+ uint16_t pos = (*namespace & 0x0FFF0000) >> 16;
+
+ switch (pos)
+ {
+ case 29: // Rascom QAF 1R
+ case 31: // Eutelsat 3B
+ {
+ pos = 30;
+ break;
+ }
+
+ case 49:
+ case 50: // SES 5
+ {
+ pos = 48; // Astra 4A
+ break;
+ }
+
+ case 215:
+ {
+ pos = 216; // Eutelsat 21B
+ break;
+ }
+
+ case 285: // Astra 2E
+ {
+ pos = 282; // Astra 2F/2G
+ break;
+ }
+
+ case 328: // Intelsat 28
+ case 329:
+ case 331: // Eutelsat 33C
+ {
+ pos = 330;
+ break;
+ }
+
+ case 359: // Eutelsat 36B
+ case 361: // Express AMU1
+ {
+ pos = 360;
+ break;
+ }
+
+ case 451: // Intelsat 904
+ {
+ pos = 450; // Intelsat 12
+ break;
+ }
+
+ case 550:
+ case 551: // G-Sat 8/16
+ {
+ pos = 549; // Yamal 402
+ break;
+ }
+
+ case 748:
+ case 749: // ABS 2A
+ {
+ pos = 750;
+ break;
+ }
+
+ case 848: // Horizons 2
+ case 852: // Intelsat 15
+ {
+ pos = 850;
+ break;
+ }
+
+ case 914: // Mesasat 3a
+ {
+ pos = 915; // Mesasat 3/3b
+ break;
+ }
+
+ case 934: // G-Sat 17
+ case 936: // Insat 4B
+ {
+ pos = 935; // G-Sat 15
+ break;
+ }
+
+ case 3600 - 911: // Nimiq 6
+ {
+ pos = 3600 - 910; // Galaxy 17
+ break;
+ }
+
+ case 3600 - 870: // SES 2
+ case 3600 - 872: // TKSat 1
+ {
+ pos = 3600 - 871;
+ break;
+ }
+
+ case 3600 - 432: // Sky Brasil 1
+ case 3600 - 430: // Intelsat 11
+ {
+ pos = 3600 - 431;
+ break;
+ }
+
+ case 3600 - 376: // Telstar 11N
+ case 3600 - 374: // NSS 10
+ {
+ pos = 3600 - 375;
+ break;
+ }
+
+ case 3600 - 359: // Hispasat 36W-1
+ {
+ pos = 3600 - 360; // Eutelsat 36 West A
+ break;
+ }
+
+ case 3600 - 81: // Eutelsat 8 West B
+ {
+ pos = 3600 - 80;
+ break;
+ }
+
+ case 3600 - 73: // Eutelsat 7 West A
+ case 3600 - 72:
+ case 3600 - 71:
+ {
+ pos = 3600 - 70; // Nilesat 201
+ break;
+ }
+
+ case 3600 - 10: // Intelsat 10-02
+ case 3600 - 9: // Thor 6
+ case 3600 - 7: // Thor 7
+ case 3600 - 6: // Thor 7
+ {
+ pos = 3600 - 8; // Thor 5
+ break;
+ }
+ }
+
+ *namespace = (*namespace & 0xF000FFFF) | (pos << 16);
+}
+
+static void annotate(char *buf, uint8_t len, const uint8_t *ecm, uint16_t ecmLen,
+ uint32_t hash, int8_t isNamespaceHash, int8_t datecoded)
+{
+ // Extract useful information to append to the "Example key ..." message.
+ //
+ // For feeds, the orbital position & frequency are usually embedded in the namespace.
+ // See https://github.com/OpenPLi/enigma2/blob/develop/lib/dvb/frontend.cpp#L476
+ // hash = (sat.orbital_position << 16);
+ // hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
+ //
+ // If the onid & tsid appear to be a unique DVB identifier, enigma2 strips the frequency
+ // from our namespace. See https://github.com/OpenPLi/enigma2/blob/develop/lib/dvb/scan.cpp#L55
+ // In that case, our annotation contains the onid:tsid:sid triplet in lieu of frequency.
+ //
+ // For the universal case, we print the number of elementary stream pids & pmtpid.
+ // The sid and current time are included for all. Examples:
+ //
+ // F 1A2B3C4D 00000000 XXXXXXXXXXXXXXXX ; 110.5W 12345H sid:0001 added: 2017-10-17 @ 13:14:15 // namespace
+ // F 1A2B3C4D 20180123 XXXXXXXXXXXXXXXX ; 33.5E ABCD:9876:1234 added: 2017-10-17 @ 13:14:15 // stripped namespace
+ // F 1A2B3C4D 20180123 XXXXXXXXXXXXXXXX ; av:5 pmt:0134 sid:0001 added: 2017-10-17 @ 13:14:15 // universal
+
+ uint8_t pidcount;
+ uint16_t frequency, degrees, pmtpid, srvid, tsid, onid;
+ uint32_t ens;
+ char compass, polarisation, timeStr1[9], timeStr2[19];
+
+ if (datecoded)
+ {
+ date_to_str(timeStr1, sizeof(timeStr1), 4, 3);
+ }
+ else
+ {
+ snprintf(timeStr1, sizeof(timeStr1), "00000000");
+ }
+
+ date_to_str(timeStr2, sizeof(timeStr2), 0, 2);
+
+ if (isNamespaceHash) // Namespace hash
+ {
+ ens = b2i(4, ecm + ecmLen - 4); // Namespace will be the last 4 bytes of the ecm
+ degrees = (ens >> 16) & 0x0FFF; // Remove not-a-pid flag
+
+ if (degrees > 1800)
+ {
+ degrees = 3600 - degrees;
+ compass = 'W';
+ }
+ else
+ {
+ compass = 'E';
+ }
+
+ if (0 == (ens & 0xFFFF)) // Stripped namespace hash
+ {
+ srvid = b2i(2, ecm + 3);
+ tsid = b2i(2, ecm + ecmLen - 8);
+ onid = b2i(2, ecm + ecmLen - 6);
+ // Printing degree sign "\u00B0" requires c99 standard
+ snprintf(buf, len, "F %08X %s XXXXXXXXXXXXXXXX ; %5.1f%c %04X:%04X:%04X added: %s",
+ hash, timeStr1, degrees / 10.0, compass, onid, tsid, srvid, timeStr2);
+ }
+ else // Full namespace hash
+ {
+ srvid = b2i(2, ecm + 3);
+ frequency = ens & 0x7FFF; // Remove polarity bit
+ polarisation = ens & 0x8000 ? 'V' : 'H';
+ // Printing degree sign "\u00B0" requires c99 standard
+ snprintf(buf, len, "F %08X %s XXXXXXXXXXXXXXXX ; %5.1f%c %5d%c sid:%04X added: %s",
+ hash, timeStr1, degrees / 10.0, compass, frequency, polarisation, srvid, timeStr2);
+ }
+ }
+ else // Universal hash
+ {
+ srvid = b2i(2, ecm + 3);
+ pmtpid = b2i(2, ecm + 5);
+ pidcount = (ecmLen - 15) / 2; // video + audio pids count
+ snprintf(buf, len, "F %08X %s XXXXXXXXXXXXXXXX ; av:%d pmt:%04X sid:%04X added: %s",
+ hash, timeStr1, pidcount, pmtpid, srvid, timeStr2);
+ }
+}
+
+static int8_t is_common_hash(uint32_t hash)
+{
+ // Check universal hash against a number of commnon universal
+ // hashes in order to warn users about potential key clashes
+
+ switch (hash)
+ {
+ case 0xBAFCD9FD: // 0001 0020 0200 1010 1020 (most common hash)
+ return 1;
+ case 0xA6A4FBD4: // 0001 0800 0200 1010 1020
+ return 1;
+ case 0xEFAB7A4D: // 0001 0800 1010 1020 0200
+ return 1;
+ case 0x83FA15D1: // 0001 0020 0134 0100 0101
+ return 1;
+ case 0x58934C38: // 0001 0800 1010 1020 1030 0200
+ return 1;
+ case 0x2C3CEC17: // 0001 0020 0134 0100
+ return 1;
+ case 0x73DF7F7E: // 0001 0020 0200 1010 1020 1030
+ return 1;
+ case 0xAFA85BC8: // 0001 0020 0021 0022 0023
+ return 1;
+ case 0x8C51F31D: // 0001 0800 0200 1010 1020 1030 1040
+ return 1;
+ case 0xE2F9BD29: // 0001 0800 0200 1010 1020 1030
+ return 1;
+ case 0xB9EBE0FF: // 0001 0100 0200 1010 1020 (less common hash)
+ return 1;
+ default:
+ return 0;
+ }
+}
+
+static int8_t is_valid_namespace(uint32_t namespace)
+{
+ // Note to developers:
+ // If we ever have a satellite at 0.0E, edit to allow stripped namespace
+ // '0xA0000000' with an additional test on tsid and onid being != 0
+
+ uint16_t orbital, frequency;
+
+ orbital = (namespace >> 16) & 0x0FFF;
+ frequency = namespace & 0x7FFF;
+
+ if ((namespace & 0xF0000000) != 0xA0000000) return 0; // Value isn't flagged as namespace
+ if ((namespace & 0x0FFFFFFF) == 0x00000000) return 0; // Empty namespace
+ if (orbital > 3599) return 0; // Allow only DVB-S
+ if (frequency == 0) return 1; // Stripped namespace
+ if (frequency >= 3400 && frequency <= 4200) return 1; // Super extended C band
+ if (frequency >= 10700 && frequency <= 12750) return 1; // Ku band Europe
+
+ return 0;
+}
+
+static int8_t get_sw(uint32_t provider, uint8_t *sw, uint8_t sw_length, int8_t dateCoded, int8_t printMsg)
+{
+ // If date-coded keys are enabled in the webif, this function evaluates the expiration date
+ // of the found keys. Expired keys are not sent to the calling function. If date-coded keys
+ // are disabled, then every key is sent without any evaluation. It takes the "provider" as
+ // input and outputs the "sw". Returns 0 (key not found, or expired) or 1 (key found).
+
+ // printMsg: 0 => No message
+ // printMsg: 1 => Print message only if key is found
+ // printMsg: 2 => Always print message, regardless if key is found or not
+
+ char keyExpDate[9] = "00000000";
+
+ if (emu_find_key('F', provider, 0, keyExpDate, sw, sw_length, 0, 0, 0, NULL)) // Key found
+ {
+ if (dateCoded) // Date-coded keys are enabled, evaluate expiration date
+ {
+ char currentDate[9];
+ date_to_str(currentDate, sizeof(currentDate), 0, 3);
+
+ if (strncmp("00000000", keyExpDate, 9) == 0 || strncmp(currentDate, keyExpDate, 9) < 0) // Evergreen or not expired
+ {
+ if (printMsg == 1 || printMsg == 2) cs_log("Key found: F %08X %s", provider, keyExpDate);
+ return 1;
+ }
+ else // Key expired
+ {
+ sw = NULL; // Make sure we don't send any expired key
+ if (printMsg == 2) cs_log("Key expired: F %08X %s", provider, keyExpDate);
+ return 0;
+ }
+ }
+ else // Date-coded keys are disabled, don't evaluate expiration date
+ {
+ if (printMsg == 1 || printMsg == 2) cs_log("Key found: F %08X %s", provider, keyExpDate);
+ return 1;
+ }
+ }
+ else // Key not found
+ {
+ if (printMsg == 2) cs_log("Key not found: F %08X", provider);
+ return 0;
+ }
+}
+
+static int8_t biss_mode1_ecm(struct s_reader *rdr, const uint8_t *ecm, uint16_t caid, uint16_t ecm_pid, uint8_t *dw, EXTENDED_CW *cw_ex)
+{
+ // Oscam's fake ecm consists of [sid] [pmtpid] [pid1] [pid2] ... [pidx] [tsid] [onid] [ens]
+ // On enigma boxes tsid, onid and namespace should be non zero, while on non-enigma
+ // boxes they are usually all zero. The top 4 bits of the namespace are flagged with 0xA.
+
+ // The emulator creates a unique channel hash using srvid and enigma namespace or
+ // srvid, tsid, onid and namespace (in case of namespace without frequency) and
+ // another weaker (not unique) hash based on every pid of the channel. This universal
+ // hash should be available on all types of stbs (enigma and non-enigma).
+
+ // Key searches are made from highest priority (tightest test first) to lowest priority
+ // (loosest test last):
+ // 1. Namespace hash (only on enigma boxes)
+ // 2. Universal hash (all box types with emu r752+)
+ // 3. Valid tsid, onid combination
+ // 4. Reverse order pid (audio, video, pmt)
+ // 5. Legacy srvid, ecm pid combination
+ // 6. Default "All Feeds" key
+
+ // If enabled in the webif, a date based key search is performed. If the expiration
+ // date has passed, the key is not sent back from get_sw(). This option is used only
+ // in the namespace hash, universal hash and the "All Feeds" search methods.
+
+ uint32_t i, ens = 0, hash = 0;
+ uint16_t srvid, tsid = 0, onid = 0, pid, ecm_len = SCT_LEN(ecm);
+ uint8_t *sw, sw_length, ecm_copy[ecm_len];
+ char tmp_buffer1[33], tmp_buffer2[90] = "0", tmp_buffer3[90] = "0";
+
+ if (caid == 0x2602 && cw_ex != NULL) // BISS2
+ {
+ cw_ex->mode = CW_MODE_ONE_CW;
+ cw_ex->algo = CW_ALGO_AES128;
+ cw_ex->algo_mode = CW_ALGO_MODE_CBC;
+ memcpy(cw_ex->data, dvb_cissa_iv, 16);
+
+ sw = cw_ex->session_word;
+ sw_length = 16;
+ }
+ else // BISS1
+ {
+ sw = dw;
+ sw_length = 8;
+ }
+
+ srvid = b2i(2, ecm + 3);
+
+ if (ecm_len >= 17) // Likely an r752+ extended ecm
+ {
+ tsid = b2i(2, ecm + ecm_len - 8);
+ onid = b2i(2, ecm + ecm_len - 6);
+ ens = b2i(4, ecm + ecm_len - 4);
+ }
+
+ // 1. Namespace hash (enigma only)
+ if (is_valid_namespace(ens))
+ {
+ unify_orbitals(&ens);
+ memcpy(ecm_copy, ecm, ecm_len);
+ i2b_buf(4, ens, ecm_copy + ecm_len - 4);
+
+ for (i = 0; i < 5; i++) // Find key matching hash made with frequency modified to: f+0, then f-1, f+1, f-2, lastly f+2
+ {
+ ecm_copy[ecm_len - 1] = (i & 1) ? ecm_copy[ecm_len - 1] - i : ecm_copy[ecm_len - 1] + i; // frequency +/- 1, 2 MHz
+
+ if (0 != (ens & 0xFFFF)) // Full namespace - Calculate hash with srvid and namespace only
+ {
+ i2b_buf(2, srvid, ecm_copy + ecm_len - 6); // Put [srvid] right before [ens]
+ hash = crc32(caid, ecm_copy + ecm_len - 6, 6);
+ }
+ else // Namespace without frequency - Calculate hash with srvid, tsid, onid and namespace
+ {
+ i2b_buf(2, srvid, ecm_copy + ecm_len - 10); // Put [srvid] right before [tsid] [onid] [ens] sequence
+ hash = crc32(caid, ecm_copy + ecm_len - 10, 10);
+ }
+
+ if (get_sw(hash, sw, sw_length, rdr->emu_datecodedenabled, i == 0 ? 2 : 1)) // Do not print "key not found" for frequency off by 1, 2
+ {
+ memcpy(sw + sw_length, sw, sw_length);
+ return EMU_OK;
+ }
+
+ if (i == 0) // No key found matching our hash: create example SoftCam.Key BISS line for the live log
+ {
+ annotate(tmp_buffer2, sizeof(tmp_buffer2), ecm_copy, ecm_len, hash, 1, rdr->emu_datecodedenabled);
+ }
+
+ if (0 == (ens & 0xFFFF)) // Namespace without frequency - Do not iterate
+ {
+ break;
+ }
+ }
+ }
+
+ // 2. Universal hash (in r752+ style ecms that contain pmt pid)
+ if ((ens & 0xF0000000) == 0xA0000000)
+ {
+ hash = crc32(caid, ecm + 3, ecm_len - 3 - 8); // Do not include [tsid] [onid] [ens] in the hash
+
+ if (get_sw(hash, sw, sw_length, rdr->emu_datecodedenabled, 2))
+ {
+ memcpy(sw + sw_length, sw, sw_length);
+ return EMU_OK;
+ }
+
+ // No key found matching our hash: create example SoftCam.Key BISS line for the live log
+ annotate(tmp_buffer3, sizeof(tmp_buffer3), ecm_copy, ecm_len, hash, 0, rdr->emu_datecodedenabled);
+ }
+
+ // 3. Valid [tsid] [onid] combination (per enigma2)
+ if (onid != 0 && (onid != 1 || tsid >= 2) && onid < 0xFF00)
+ {
+ if (get_sw(tsid << 16 | onid, sw, sw_length, 0, 2))
+ {
+ memcpy(sw + sw_length, sw, sw_length);
+ return EMU_OK;
+ }
+ }
+
+ // 4. Reverse order pid search
+ // (better identifies channels with variable counts of audio pids)
+ // Strip [tsid] [onid] [ens] on r752+ ecms to be compatible with older versions)
+ if ((ens & 0xF0000000) == 0xA0000000)
+ {
+ ecm_len -= 8;
+ }
+
+ for (i = ecm_len - 2; i >= 5; i -= 2)
+ {
+ pid = b2i(2, ecm + i);
+
+ if (get_sw((srvid << 16) | pid, sw, sw_length, 0, 2))
+ {
+ memcpy(sw + sw_length, sw, sw_length);
+ return EMU_OK;
+ }
+ }
+
+ // 5. Legacy [srvid] [ecm pid] combination
+ if (get_sw((srvid << 16) | ecm_pid, sw, sw_length, 0, 2))
+ {
+ memcpy(sw + sw_length, sw, sw_length);
+ return EMU_OK;
+ }
+
+ // 6. Default BISS key for events with many feeds sharing the same session word
+ // (limited to local ecms, network ecms with ecm pid equal to zero are blocked)
+ if (ecm_pid != 0 && get_sw(0xA11FEED5, sw, sw_length, rdr->emu_datecodedenabled, 2))
+ {
+ memcpy(sw + sw_length, sw, sw_length);
+ cs_hexdump(0, sw, sw_length, tmp_buffer1, sizeof(tmp_buffer1));
+ cs_log("No specific match found. Using 'All Feeds' key: %s", tmp_buffer1);
+ return EMU_OK;
+ }
+
+ // Print example key lines for available hash search methods, if no key is found
+ if (strncmp(tmp_buffer2, "0", 2)) cs_log("Example key based on namespace hash: %s", tmp_buffer2);
+ if (strncmp(tmp_buffer3, "0", 2)) cs_log("Example key based on universal hash: %s", tmp_buffer3);
+
+ // Check if universal hash is common and warn user
+ if (is_common_hash(hash)) cs_log("Feed has commonly used pids, universal hash clashes in SoftCam.Key are likely!");
+
+ return EMU_KEY_NOT_FOUND;
+}
+
+static inline int8_t get_ecm_key(uint16_t onid, uint16_t esid, uint8_t parity, uint8_t *key)
+{
+ return emu_find_key('G', onid << 16 | esid, 0, parity == 0 ? "00" : "01", key, 16, 1, 0, 0, NULL);
+}
+
+static int8_t biss2_mode_ca_ecm(const uint8_t *ecm, EXTENDED_CW *cw_ex)
+{
+ uint8_t ecm_cipher_type, session_key_parity;
+ uint8_t session_key[16], iv[16];
+ uint16_t entitlement_session_id, original_network_id, descriptor_length;
+ uint16_t position, ecm_length = SCT_LEN(ecm);
+ uint32_t payload_checksum, calculated_checksum;
+ char tmp_buffer[64];
+ struct aes_keys aes;
+
+ // Calculate crc32 checksum and compare against the checksum bytes of the ECM
+ payload_checksum = b2i(4, ecm + ecm_length - 4);
+ calculated_checksum = ccitt32_crc((uint8_t *)ecm, ecm_length - 4);
+
+ if (payload_checksum != calculated_checksum)
+ {
+ cs_log_dbg(D_TRACE, "ECM checksum mismatch (payload: %08X vs calculated: %08X",
+ payload_checksum, calculated_checksum);
+ return EMU_CHECKSUM_ERROR;
+ }
+
+ // Unique identifiers of the session key
+ entitlement_session_id = b2i(2, ecm + 3);
+ original_network_id = b2i(2, ecm + 8);
+
+ ecm_cipher_type = ecm[10] >> 5;
+ if (ecm_cipher_type != 0) // Session words shall be encrypted with AES_128_CBC
+ {
+ cs_log("ECM cipher type %d not supported", ecm_cipher_type);
+ return EMU_NOT_SUPPORTED;
+ }
+
+ descriptor_length = b2i(2, ecm + 10) & 0x0FFF;
+ position = 12 + descriptor_length;
+
+ session_key_parity = ecm[position] >> 7; // Parity can be "00" or "01"
+ position++;
+
+ if (!get_ecm_key(original_network_id, entitlement_session_id, session_key_parity, session_key))
+ {
+ return EMU_KEY_NOT_FOUND;
+ }
+
+ memcpy(iv, ecm + position, 16); // "AES_128_CBC_enc_session_word_iv"
+ memcpy(cw_ex->session_word, ecm + position + 16, 16); // "AES_128_CBC_enc_session_word_0"
+ memcpy(cw_ex->session_word + 16, ecm + position + 32, 16); // "AES_128_CBC_enc_session_word_1"
+
+ // Delete these cs_log calls when everything is confirmed to work correctly
+ cs_hexdump(3, iv, 16, tmp_buffer, sizeof(tmp_buffer));
+ cs_log_dbg(D_TRACE, "session_word_iv: %s", tmp_buffer);
+
+ cs_hexdump(3, cw_ex->session_word, 16, tmp_buffer, sizeof(tmp_buffer));
+ cs_log_dbg(D_TRACE, "encrypted session_word_0: %s", tmp_buffer);
+
+ cs_hexdump(3, cw_ex->session_word + 16, 16, tmp_buffer, sizeof(tmp_buffer));
+ cs_log_dbg(D_TRACE, "encrypted session_word_1: %s", tmp_buffer);
+
+ // Decrypt session words
+ aes_set_key(&aes, (char *)session_key);
+ aes_cbc_decrypt(&aes, cw_ex->session_word, 16, iv);
+ memcpy(iv, ecm + position, 16); // Set iv again to the correct one
+ aes_cbc_decrypt(&aes, cw_ex->session_word + 16, 16, iv);
+
+ // Delete these cs_log calls when everything is confirmed to work correctly
+ cs_hexdump(3, cw_ex->session_word, 16, tmp_buffer, sizeof(tmp_buffer));
+ cs_log_dbg(D_TRACE, "decrypted session_word_0: %s", tmp_buffer);
+
+ cs_hexdump(3, cw_ex->session_word + 16, 16, tmp_buffer, sizeof(tmp_buffer));
+ cs_log_dbg(D_TRACE, "decrypted session_word_1: %s", tmp_buffer);
+
+ cw_ex->mode = CW_MODE_ONE_CW;
+ cw_ex->algo = CW_ALGO_AES128;
+ cw_ex->algo_mode = CW_ALGO_MODE_CBC;
+ memcpy(cw_ex->data, dvb_cissa_iv, 16);
+
+ return EMU_OK;
+}
+
+int8_t biss_ecm(struct s_reader *rdr, const uint8_t *ecm, uint16_t caid, uint16_t ecm_pid, uint8_t *dw, EXTENDED_CW *cw_ex)
+{
+ switch (caid)
+ {
+ case 0x2600:
+ return biss_mode1_ecm(rdr, ecm, caid, ecm_pid, dw, NULL);
+
+ case 0x2602:
+ return biss_mode1_ecm(rdr, ecm, caid, ecm_pid, NULL, cw_ex);
+
+ case 0x2610:
+ return biss2_mode_ca_ecm(ecm, cw_ex);
+
+ default:
+ cs_log("Unknown Biss caid %04X - Please report!", caid);
+ return EMU_NOT_SUPPORTED;
+ }
+}
+
+static uint16_t parse_session_data_descriptor(const uint8_t *data, uint16_t esid, uint16_t onid, uint32_t *keysAdded)
+{
+ uint8_t descriptor_tag = data[0];
+ uint8_t descriptor_length = data[1];
+
+ switch (descriptor_tag)
+ {
+ case 0x81: // session_key_descriptor
+ {
+ uint8_t session_key_type = data[2] >> 1;
+ if (session_key_type == 0) // AES-128
+ {
+ uint8_t session_key_parity = data[2] & 0x01;
+ uint8_t session_key_data[16];
+ memcpy(session_key_data, data + 3, 16); // This is the ECM key
+
+ SAFE_MUTEX_LOCK(&emu_key_data_mutex);
+ if (emu_update_key('G', onid << 16 | esid, session_key_parity ? "01" : "00", session_key_data, 16, 1, NULL))
+ {
+ (*keysAdded)++;
+ char tmp[33];
+ cs_hexdump(0, session_key_data, 16, tmp, sizeof(tmp));
+ cs_log("Key found in EMM: G %08X %02d %s", onid << 16 | esid, session_key_parity, tmp);
+ }
+ SAFE_MUTEX_UNLOCK(&emu_key_data_mutex);
+ }
+ break;
+ }
+
+ case 0x82: // entitlement_flags_descriptor
+ break;
+
+ default:
+ break;
+ }
+
+ return 2 + descriptor_length;
+}
+
+static int8_t parse_session_data(const uint8_t *data, RSA *key, uint16_t esid, uint16_t onid, uint32_t *keysAdded)
+{
+ // session_data is encrypted with RSA 2048 bit OAEP
+ // Maximum size of decrypted session_data is less than (256-41) bytes
+ uint8_t session_data[214];
+
+ if (RSA_private_decrypt(256, data, session_data, key, RSA_PKCS1_OAEP_PADDING) > 0)
+ {
+ uint16_t pos = 0;
+ uint16_t descriptor_length = b2i(2, session_data) & 0x0FFF;
+
+ while (pos < descriptor_length)
+ {
+ pos += parse_session_data_descriptor(session_data + 2 + pos, esid, onid, keysAdded);
+ }
+
+ return EMU_OK;
+ }
+
+ return EMU_NOT_SUPPORTED; // Decryption failed for whatever reason
+}
+
+static int8_t get_rsa_key(struct s_reader *rdr, const uint8_t *ekid, RSA **key)
+{
+ LL_ITER itr;
+ biss2_rsa_key_t *data;
+
+ itr = ll_iter_create(rdr->ll_biss2_rsa_keys);
+ while ((data = ll_iter_next(&itr)))
+ {
+ if (data->ekid == ekid)
+ {
+ *key = data->key;
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int8_t biss_emm(struct s_reader *rdr, const uint8_t *emm, uint32_t *keysAdded)
+{
+ uint8_t emm_cipher_type, entitlement_priv_data_loop, entitlement_key_id[8];
+ uint16_t entitlement_session_id, original_network_id, descriptor_length;
+ uint16_t pos, emm_length = SCT_LEN(emm);
+ uint32_t payload_checksum, calculated_checksum;
+ int8_t result = EMU_NOT_SUPPORTED;
+ char tmp[17];