-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
ca.c
3030 lines (2592 loc) · 91.6 KB
/
ca.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
/*
Minisatip does not assume any responsability related to functionality of this
code. It also does not include any certificates, please procure them from
alternative source
*/
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "adapter.h"
#include "ca.h"
#include "dvb.h"
#include "dvbapi.h"
#include "minisatip.h"
#include "poll.h"
#include "search.h"
#include "socketworks.h"
#include "tables.h"
#include <linux/dvb/ca.h>
#include "api/symbols.h"
#include "api/variables.h"
#include "utils.h"
#include "utils/ticks.h"
#define DEFAULT_LOG LOG_DVBCA
#define EN50221_APP_RM_RESOURCEID MKRID(1, 1, 1)
#define EN50221_APP_AI_RESOURCEID MKRID(2, 1, 1)
#define EN50221_APP_CA_RESOURCEID MKRID(3, 1, 1)
#define EN50221_APP_DATETIME_RESOURCEID MKRID(36, 1, 1)
#define EN50221_APP_MMI_RESOURCEID MKRID(64, 1, 1)
#define EN50221_APP_DVB_RESOURCEID MKRID(32, 1, 1)
#define TS101699_APP_RM_RESOURCEID MKRID(1, 1, 2)
#define TS101699_APP_AI_RESOURCEID MKRID(2, 1, 2)
#define CIPLUS_APP_AI_RESOURCEID MKRID(2, 1, 3)
#define CIPLUS_APP_AI_RESOURCEID_MULTI MKRID(2, 1, 4)
#define BLUEBOOK_A173_2_APP_AI_RESOURCE_ID MKRID(2, 1, 5)
#define CIPLUS_APP_DVB_RESOURCEID MKRID(32, 1, 2)
#define TS103205_APP_DVB_RESOURCEID MKRID(32, 1, 3)
#define TS103205_APP_DVB_MULTISTREAM_RESOURCEID MKRID(32, 2, 1)
#define TS103205_APP_CA_MULTISTREAM_RESOURCEID MKRID(3, 2, 1)
#define TS103205_APP_MMI_RESOURCEID MKRID(64, 2, 1)
#define CIPLUS_APP_CC_RESOURCEID MKRID(140, 64, 1)
#define CIPLUS_APP_CC_RESOURCEID_TWO MKRID(140, 64, 2)
#define TS103205_APP_CC_RESOURCEID_THREE MKRID(140, 64, 4)
#define TS103205_APP_CC_MULTISTREAM_RESOURCEID MKRID(140, 65, 1)
#define CIPLUS_APP_LANG_RESOURCEID MKRID(141, 64, 1)
#define CIPLUS_APP_UPGR_RESOURCEID MKRID(142, 64, 1)
#define CIPLUS_APP_OPRF_RESOURCEID MKRID(143, 64, 1)
#define TS103205_APP_OPRF_TWO_RESOURCEID MKRID(143, 64, 2)
#define TS103205_APP_OPRF_THREE_RESOURCEID MKRID(143, 64, 3)
#define TS103205_APP_MULTISTREAM_RESOURCEID MKRID(144, 1, 1)
#define CIPLUS_APP_SAS_RESOURCEID MKRID(150, 64, 1)
#define TS101699_APP_AMMI_RESOURCEID MKRID(65, 1, 1)
#define CIPLUS_APP_AMMI_RESOURCEID MKRID(65, 1, 2)
#define TS103205_APP_AMMI_RESOURCEID MKRID(65, 2, 1)
char ci_name_underscore[128];
int ci_number;
int logging = 0;
char logfile[256];
int extract_ci_cert = 0;
uint32_t datatype_sizes[MAX_ELEMENTS] = {
0, 50, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 32, 256, 256, 0, 0,
256, 256, 32, 8, 8, 32, 32, 0, 8, 2, 32, 1, 32, 1, 0, 32};
int dvbca_id;
ca_device_t *ca_devices[MAX_ADAPTERS];
typedef enum {
CIPLUS_DATA_RATE_72_MBPS = 0,
CIPLUS_DATA_RATE_96_MBPS = 1,
} ciplus13_data_rate_t;
unsigned char dh_p[256] =
{/* prime */
0xd6, 0x27, 0x14, 0x7a, 0x7c, 0x0c, 0x26, 0x63, 0x9d, 0x82, 0xeb, 0x1f,
0x4a, 0x18, 0xff, 0x6c, 0x34, 0xad, 0xea, 0xa6, 0xc0, 0x23, 0xe6, 0x65,
0xfc, 0x8e, 0x32, 0xc3, 0x33, 0xf4, 0x91, 0xa7, 0xcc, 0x88, 0x58, 0xd7,
0xf3, 0xb3, 0x17, 0x5e, 0xb0, 0xa8, 0xeb, 0x5c, 0xd4, 0xd8, 0x3a, 0xae,
0x8e, 0x75, 0xa1, 0x50, 0x5f, 0x5d, 0x67, 0xc5, 0x40, 0xf4, 0xb3, 0x68,
0x35, 0xd1, 0x3a, 0x4c, 0x93, 0x7f, 0xca, 0xce, 0xdd, 0x83, 0x29, 0x01,
0xc8, 0x4b, 0x76, 0x81, 0x56, 0x34, 0x83, 0x31, 0x92, 0x72, 0x65, 0x7b,
0xac, 0xd9, 0xda, 0xa9, 0xd1, 0xd3, 0xe5, 0x77, 0x58, 0x6f, 0x5b, 0x44,
0x3e, 0xaf, 0x7f, 0x6d, 0xf5, 0xcf, 0x0a, 0x80, 0x0d, 0xa5, 0x56, 0x4f,
0x4b, 0x85, 0x41, 0x0f, 0x13, 0x41, 0x06, 0x1f, 0xf3, 0xd9, 0x65, 0x36,
0xae, 0x47, 0x41, 0x1f, 0x1f, 0xe0, 0xde, 0x69, 0xe5, 0x86, 0x2a, 0xa1,
0xf2, 0x48, 0x02, 0x92, 0x68, 0xa6, 0x37, 0x9f, 0x76, 0x4f, 0x7d, 0x94,
0x5d, 0x10, 0xe5, 0xab, 0x5d, 0xb2, 0xf3, 0x12, 0x8c, 0x79, 0x03, 0x92,
0xa6, 0x7f, 0x8a, 0x78, 0xb0, 0xba, 0xc5, 0xb5, 0x31, 0xc5, 0xc8, 0x22,
0x6e, 0x29, 0x02, 0x40, 0xab, 0xe7, 0x5c, 0x23, 0x33, 0x7f, 0xcb, 0x86,
0xc7, 0xb4, 0xfd, 0xaa, 0x44, 0xcd, 0x9c, 0x9f, 0xba, 0xac, 0x3a, 0xcf,
0x7e, 0x31, 0x5f, 0xa8, 0x47, 0xce, 0xca, 0x1c, 0xb4, 0x77, 0xa0, 0xec,
0x9a, 0x46, 0xd4, 0x79, 0x7b, 0x64, 0xbb, 0x6c, 0x91, 0xb2, 0x38, 0x01,
0x65, 0x11, 0x45, 0x9f, 0x62, 0x08, 0x6f, 0x31, 0xcf, 0xc4, 0xba, 0xdc,
0xd0, 0x03, 0x91, 0xf1, 0x18, 0x1f, 0xcb, 0x4d, 0xfc, 0x73, 0x5a, 0xa2,
0x15, 0xb8, 0x3c, 0x8d, 0x80, 0x92, 0x1c, 0xa1, 0x03, 0xd0, 0x83, 0x2f,
0x5f, 0xe3, 0x07, 0x69};
unsigned char dh_g[256] =
{/* generator */
0x95, 0x7d, 0xd1, 0x49, 0x68, 0xc1, 0xa5, 0xf1, 0x48, 0xe6, 0x50, 0x4f,
0xa1, 0x10, 0x72, 0xc4, 0xef, 0x12, 0xec, 0x2d, 0x94, 0xbe, 0xc7, 0x20,
0x2c, 0x94, 0xf9, 0x68, 0x67, 0x0e, 0x22, 0x17, 0xb5, 0x5c, 0x0b, 0xca,
0xac, 0x9f, 0x25, 0x9c, 0xd2, 0xa6, 0x1a, 0x20, 0x10, 0x16, 0x6a, 0x42,
0x27, 0x83, 0x47, 0x42, 0xa0, 0x07, 0x52, 0x09, 0x33, 0x97, 0x4e, 0x30,
0x57, 0xd8, 0xb7, 0x1e, 0x46, 0xa6, 0xba, 0x4e, 0x40, 0x6a, 0xe9, 0x1a,
0x5a, 0xa0, 0x74, 0x56, 0x92, 0x55, 0xc2, 0xbd, 0x44, 0xcd, 0xb3, 0x33,
0xf7, 0x35, 0x46, 0x25, 0xdf, 0x84, 0x19, 0xf3, 0xe2, 0x7a, 0xac, 0x4e,
0xee, 0x1a, 0x86, 0x3b, 0xb3, 0x87, 0xa6, 0x66, 0xc1, 0x70, 0x21, 0x41,
0xd3, 0x58, 0x36, 0xb5, 0x3b, 0x6e, 0xa1, 0x55, 0x60, 0x9a, 0x59, 0xd3,
0x85, 0xd8, 0xdc, 0x6a, 0xff, 0x41, 0xb6, 0xbf, 0x42, 0xde, 0x64, 0x00,
0xd0, 0xee, 0x3a, 0xa1, 0x8a, 0xed, 0x12, 0xf9, 0xba, 0x54, 0x5c, 0xdb,
0x06, 0x24, 0x49, 0xe8, 0x47, 0xcf, 0x5b, 0xe4, 0xbb, 0xc0, 0xaa, 0x8a,
0x8c, 0xbe, 0x73, 0xd9, 0x02, 0xea, 0xee, 0x8d, 0x87, 0x5b, 0xbf, 0x78,
0x04, 0x41, 0x9e, 0xa8, 0x5c, 0x3c, 0x49, 0xde, 0x88, 0x6d, 0x62, 0x21,
0x7f, 0xf0, 0x5e, 0x2d, 0x1d, 0xfc, 0x47, 0x0d, 0x1b, 0xaa, 0x4e, 0x0d,
0x78, 0x20, 0xfe, 0x57, 0x0f, 0xca, 0xdf, 0xeb, 0x3c, 0x84, 0xa7, 0xe1,
0x61, 0xb2, 0x95, 0x98, 0x07, 0x73, 0x8e, 0x51, 0xc6, 0x87, 0xe4, 0xcf,
0xf1, 0x5f, 0x86, 0x99, 0xec, 0x8d, 0x44, 0x92, 0x2c, 0x99, 0xf6, 0xc0,
0xf4, 0x39, 0xe8, 0x05, 0xbf, 0xc1, 0x56, 0xde, 0xfe, 0x93, 0x75, 0x06,
0x69, 0x87, 0x83, 0x06, 0x51, 0x80, 0xa5, 0x6e, 0xa6, 0x19, 0x7d, 0x3b,
0xef, 0xfb, 0xe0, 0x4a};
static int ca_send_datetime(ca_device_t *d);
int populate_resources(ca_device_t *d, int *resource_ids);
int ca_write_apdu(ca_session_t *s, int resource, const void *data, int len);
ca_session_t *find_session_for_resource(ca_device_t *d, int resource);
int asn_1_decode(int *length, unsigned char *asn_1_array);
////// MISC.C
int add_padding(uint8_t *dest, unsigned int len, unsigned int blocklen) {
uint8_t padding = 0x80;
int count = 0;
while (len & (blocklen - 1)) {
*dest++ = padding;
++len;
++count;
padding = 0;
}
return count;
}
static int get_bin_from_nibble(int in) {
if ((in >= '0') && (in <= '9'))
return in - 0x30;
if ((in >= 'A') && (in <= 'Z'))
return in - 0x41 + 10;
if ((in >= 'a') && (in <= 'z'))
return in - 0x61 + 10;
LOG("fixme: unsupported chars in hostid");
return 0;
}
void str2bin(uint8_t *dst, char *data, int len) {
int i;
for (i = 0; i < len; i += 2)
*dst++ = (get_bin_from_nibble(data[i]) << 4) |
get_bin_from_nibble(data[i + 1]);
}
void cert_strings(char *certfile) {
int c;
unsigned count;
// off_t offset;
FILE *file;
char string[256];
int n = 2; /* too short string to be usefull */
int line = 0;
file = fopen(certfile, "r");
if (!file) {
LOG("Could not open certificate file %s", certfile);
return;
}
LOG("#########################################################\n");
// offset = 0;
count = 0;
do {
if (line > 14)
n = 8; /* after usefull info be stricter */
c = fgetc(file);
// if (isprint(c) || c == '\t')
if (isprint(c)) {
string[count] = c;
count++;
} else {
if (count > n) /* line feed */
{
string[count - 1] = 0;
LOG("%s\n", string);
line++;
}
count = 0;
}
// offset++;
} while ((c != EOF) && (line < 16)); /* only frst 15 lines */
fclose(file);
LOG("#########################################################\n");
return;
}
/// END MISC
void ca_request_close(ca_device_t *d) {
LOG("Requesting CA %d close", d->id);
sockets_force_close(d->sock);
}
int is_ca_initializing(int i) {
if (i >= 0 && i < MAX_ADAPTERS && ca_devices[i] && ca_devices[i]->enabled &&
ca_devices[i]->state != CA_STATE_INACTIVE &&
ca_devices[i]->state != CA_STATE_INITIALIZED)
return 1;
return 0;
}
int get_max_pmt_for_ca(int i) {
if (i >= 0 && i < MAX_ADAPTERS && ca_devices[i] && ca_devices[i]->enabled)
return (ca_devices[i]->multiple_pmt + 1) * ca_devices[i]->max_ca_pmt;
return MAX_CA_PMT;
}
void send_cw_to_all_pmts(ca_device_t *d, int parity) {
int i;
for (i = 0; i < d->max_ca_pmt; i++) {
if (PMT_ID_IS_VALID(d->capmt[i].pmt_id)) {
send_cw(d->capmt[i].pmt_id, CA_ALGO_AES128_CBC, parity,
d->key[parity], d->iv[parity], 3720, NULL);
}
if (PMT_ID_IS_VALID(d->capmt[i].other_id)) {
send_cw(d->capmt[i].other_id, CA_ALGO_AES128_CBC, parity,
d->key[parity], d->iv[parity], 3720, NULL);
}
}
}
void disable_cws_for_all_pmts(ca_device_t *d) {
int i;
for (i = 0; i < d->max_ca_pmt; i++) {
if (PMT_ID_IS_VALID(d->capmt[i].pmt_id)) {
disable_cw(d->capmt[i].pmt_id);
}
if (PMT_ID_IS_VALID(d->capmt[i].other_id)) {
disable_cw(d->capmt[i].other_id);
}
}
}
int create_capmt(SCAPMT *ca, int listmgmt, uint8_t *capmt, int capmt_len,
int cmd_id) {
int pos = 0;
SPMT *pmt = get_pmt(ca->pmt_id);
SPMT *other = get_pmt(ca->other_id);
int version = ca->version;
if (!pmt)
LOG_AND_RETURN(1, "%s: PMT %d not found", __FUNCTION__, ca->pmt_id);
capmt[pos++] = listmgmt;
copy16(capmt, pos, ca->sid);
pos += 2;
capmt[pos++] = ((version & 0xF) << 1) | 0x1;
capmt[pos++] = 0; // PI LEN 2 bytes, set 0
capmt[pos++] = 0;
pos += CAPMT_add_PMT(capmt + pos, capmt_len - pos, pmt, cmd_id);
if (other) {
pos += CAPMT_add_PMT(capmt + pos, capmt_len - pos, other, cmd_id);
}
return pos;
}
// Creates and Sends 2 PMTs to bundle them into the same CAPMT
// Second PMT can be NULL
int send_capmt(ca_device_t *d, SCAPMT *ca, int listmgmt, int reason) {
uint8_t capmt[8192];
memset(capmt, 0, sizeof(capmt));
int size = create_capmt(ca, listmgmt, capmt, sizeof(capmt), reason);
hexdump("CAPMT: ", capmt, size);
if (size <= 0)
LOG_AND_RETURN(TABLES_RESULT_ERROR_NORETRY, "create_capmt failed");
ca_session_t *session =
find_session_for_resource(d, EN50221_APP_CA_RESOURCEID);
if (!session)
LOG_AND_RETURN(1, "Unable to find CA session for device %d", d->id);
ca_write_apdu(session, TAG_CA_PMT, capmt, size);
return 0;
}
int get_enabled_pmts_for_ca(ca_device_t *d) {
int i, enabled_pmts = 0;
for (i = 0; i < d->max_ca_pmt; i++) {
if (PMT_ID_IS_VALID(d->capmt[i].pmt_id))
enabled_pmts++;
if (PMT_ID_IS_VALID(d->capmt[i].other_id))
enabled_pmts++;
}
return enabled_pmts;
}
SCAPMT *add_pmt_to_capmt(ca_device_t *d, SPMT *pmt, int multiple) {
int ca_pos;
SCAPMT *res = NULL;
for (ca_pos = 0; ca_pos < d->max_ca_pmt; ca_pos++) {
if (d->capmt[ca_pos].pmt_id == pmt->id ||
d->capmt[ca_pos].other_id == pmt->id) {
res = d->capmt + ca_pos;
break;
}
}
if (!res) {
for (ca_pos = 0; ca_pos < d->max_ca_pmt; ca_pos++) {
if (d->capmt[ca_pos].pmt_id == -1) {
d->capmt[ca_pos].pmt_id = pmt->id;
res = d->capmt + ca_pos;
break;
}
if (multiple && d->capmt[ca_pos].other_id == -1) {
d->capmt[ca_pos].other_id = pmt->id;
res = d->capmt + ca_pos;
break;
}
}
}
if (res) {
res->version = (res->version + 1) & 0xF;
} else {
LOG("CA %d all channels used %d, multiple allowed %d", d->id,
d->max_ca_pmt, multiple);
for (ca_pos = 0; ca_pos < d->max_ca_pmt; ca_pos++) {
LOG("CAPMT %d pmts %d %d", ca_pos, d->capmt[ca_pos].pmt_id,
d->capmt[ca_pos].other_id);
}
}
return res;
}
int get_active_capmts(ca_device_t *d) {
int i, active = 0;
for (i = 0; i < d->max_ca_pmt; i++)
if (PMT_ID_IS_VALID(d->capmt[i].pmt_id) ||
PMT_ID_IS_VALID(d->capmt[i].other_id))
active++;
return active;
}
void set_input_ci(int ci, int adapter) {
char filename[100];
char val = 'A' + adapter;
sprintf(filename, "/proc/stb/tsmux/ci%d_input", ci);
LOG("%s: %s -> %c", __FUNCTION__, filename, val);
FILE *fp = fopen(filename, "w");
if (fp) {
fprintf(fp, "%c", val);
fclose(fp);
} else {
LOG("%s open failed, %s", __FUNCTION__, filename);
}
}
void set_tuner_input(int ci, int adapter) {
char filename[100];
char val[] = "CI0";
if (adapter == -1)
return;
if (ci == -1) {
val[0] = 'A' + adapter;
val[1] = 0;
} else
val[2] = '0' + ci;
sprintf(filename, "/proc/stb/tsmux/input%d", adapter);
LOG("%s: %s -> %s", __FUNCTION__, filename, val);
FILE *fp = fopen(filename, "w");
if (fp) {
fprintf(fp, "%s", val);
fclose(fp);
} else {
LOG("%s: open failed, %s", __FUNCTION__, filename);
}
}
int any_ca_initializing() {
int i;
for (i = 0; i < MAX_ADAPTERS; i++)
if (is_ca_initializing(i))
return 1;
return 0;
}
ca_device_t *find_dvbca_for_pmt(SPMT *pmt) {
ca_device_t *d;
extern SCA ca[MAX_CA];
int i, j;
for (i = 0; i < MAX_ADAPTERS; i++)
if (ca_devices[i] && ca_devices[i]->state == CA_STATE_INITIALIZED) {
d = ca_devices[i];
for (j = 0; j < ca[dvbca_id].ad_info[i].caids; j++)
if (match_caid(pmt, ca[dvbca_id].ad_info[i].caid[j],
ca[dvbca_id].ad_info[i].mask[j])) {
LOG("Found CA %d for PMT %d", i, pmt->id);
return d;
}
}
return NULL;
}
// Sending _LIST, _MORE and _LAST leads to decryption issues on some CAMs
// (existing channels are restarted) This method tries to send _ONLY for the
// first PMT or when the sid changes (when 2 PMTs are packed inside the same
// CAPMT) and uses just _UPDATE to update the existing and new CAPMT
// According to the docs _ADD should be used for new CAPMTs but _UPDATE with
// a new sid works To not leak sids, the first channel will have the
// original SID (some CAMs do not like this), the other ones a fixed SID
int dvbca_process_pmt(adapter *ad, SPMT *spmt) {
ca_device_t *d = ca_devices[ad->id];
uint16_t pid, sid, ver;
int listmgmt;
SPMT *first = NULL;
if (opts.enigma) {
// detach the CI from the previous adapter
if (d->linked_adapter != ad->id) {
set_tuner_input(-1, d->linked_adapter);
}
// enigma allows attaching the CI to the adapter that needs it
d = find_dvbca_for_pmt(spmt);
if (!d) {
if (any_ca_initializing())
return TABLES_RESULT_ERROR_RETRY;
else
return TABLES_RESULT_ERROR_NORETRY;
}
set_tuner_input(d->id, ad->id);
set_input_ci(d->id, ad->id);
d->linked_adapter = ad->id;
}
if (!d)
return TABLES_RESULT_ERROR_NORETRY;
if (d->state != CA_STATE_INITIALIZED)
LOG_AND_RETURN(TABLES_RESULT_ERROR_RETRY, "CAM not yet initialized");
SCAPMT *capmt = add_pmt_to_capmt(d, spmt, d->multiple_pmt);
if (!capmt)
LOG_AND_RETURN(TABLES_RESULT_ERROR_RETRY,
"No free slots to add PMT %d to CA %d", spmt->id, d->id);
first = get_pmt(capmt->pmt_id);
if (!first)
LOG_AND_RETURN(TABLES_RESULT_ERROR_NORETRY,
"First PMT not found from capmt %d", capmt->pmt_id);
pid = spmt->pid;
ver = capmt->version;
sid = spmt->sid;
listmgmt = get_active_capmts(d) == 1 ? CLM_ONLY : CLM_UPDATE;
if (listmgmt == CLM_ONLY) {
if (capmt->sid == first->sid)
listmgmt = CLM_UPDATE;
capmt->sid = first->sid;
int i;
for (i = 0; i < d->max_ca_pmt; i++) {
int sid = MAKE_SID_FOR_CA(d->id, i);
if (capmt != d->capmt + i) {
if (capmt->sid == sid)
d->capmt[i].sid = MAKE_SID_FOR_CA(d->id, MAX_CA_PMT);
else
d->capmt[i].sid = sid;
}
}
}
LOG("PMT CA %d pmt %d pid %u (%s) ver %u sid %u (%d), "
"enabled_pmts %d, "
"%s, PMTS to be send %d %d, pos %ld",
spmt->adapter, spmt->id, pid, spmt->name, ver, sid, capmt->sid,
get_enabled_pmts_for_ca(d), listmgmt_str[listmgmt], capmt->pmt_id,
capmt->other_id, capmt - d->capmt);
if (send_capmt(d, capmt, listmgmt, CA_PMT_CMD_ID_OK_DESCRAMBLING))
LOG_AND_RETURN(TABLES_RESULT_ERROR_NORETRY, "send_capmt failed");
if (d->key[0][0])
send_cw(spmt->id, CA_ALGO_AES128_CBC, 0, d->key[0], d->iv[0], 3600,
NULL); // 1 hour
if (d->key[1][0])
send_cw(spmt->id, CA_ALGO_AES128_CBC, 1, d->key[1], d->iv[1], 3600,
NULL);
return 0;
}
// Remove the PMT from the CAPMT associated with this device
// if the pmt_id is not valid and other_id is valid,
// exchange the IDs
void remove_pmt_from_device(ca_device_t *d, SPMT *pmt) {
int i;
for (i = 0; i < d->max_ca_pmt; i++) {
if (d->capmt[i].pmt_id == pmt->id) {
d->capmt[i].pmt_id = PMT_INVALID;
if (PMT_ID_IS_VALID(d->capmt[i].other_id)) {
d->capmt[i].pmt_id = d->capmt[i].other_id;
d->capmt[i].other_id = PMT_INVALID;
d->capmt[i].version++;
}
}
if (d->capmt[i].other_id == pmt->id) {
d->capmt[i].other_id = PMT_INVALID;
d->capmt[i].version++;
}
}
return;
}
SCAPMT *get_capmt_for_pmt(SPMT *pmt, ca_device_t **output) {
ca_device_t *d;
int i, j;
for (i = 0; i < MAX_ADAPTERS; i++)
if ((d = ca_devices[i]) &&
(d->enabled && d->state == CA_STATE_INITIALIZED)) {
for (j = 0; j < d->max_ca_pmt; j++)
if (d->capmt[j].pmt_id == pmt->id ||
d->capmt[j].other_id == pmt->id) {
*output = d;
return d->capmt + j;
}
}
return NULL;
}
int dvbca_del_pmt(adapter *ad, SPMT *spmt) {
ca_device_t *d = ca_devices[ad->id];
int listmgmt = -1;
SCAPMT *capmt = get_capmt_for_pmt(spmt, &d);
if (!capmt)
LOG_AND_RETURN(0, "CAPMT not found for pmt %d", spmt->id);
remove_pmt_from_device(d, spmt);
if (PMT_ID_IS_VALID(capmt->pmt_id)) {
listmgmt = CLM_UPDATE;
if (send_capmt(d, capmt, listmgmt, CA_PMT_CMD_ID_OK_DESCRAMBLING))
LOG_AND_RETURN(TABLES_RESULT_ERROR_NORETRY,
"%s: send_capmt failed for pmt ids %d", __FUNCTION__,
capmt->pmt_id)
}
LOG("PMT CA %d DEL pmt %d pid %u, sid %u (%u), ver %d, %s name: %s",
spmt->adapter, spmt->id, spmt->pid, spmt->sid, capmt->sid,
capmt->version, listmgmt >= 0 ? listmgmt_str[listmgmt] : "NO UPDATE",
spmt->name);
return 0;
}
static int ciplus13_app_ai_data_rate_info(ca_session_t *s,
ciplus13_data_rate_t rate) {
LOG("setting CI+ CAM data rate to %s Mbps", rate ? "96" : "72");
ca_write_apdu(s, CI_DATA_RATE_INFO, &rate, 1);
return 0;
}
static struct element *element_get(struct cc_ctrl_data *cc_data,
unsigned int id) {
/* array index */
if ((id < 1) || (id >= MAX_ELEMENTS)) {
LOG("element_get: invalid id");
return NULL;
}
return &cc_data->elements[id];
}
static void element_invalidate(struct cc_ctrl_data *cc_data, unsigned int id) {
struct element *e;
e = element_get(cc_data, id);
if (e) {
free(e->data);
memset(e, 0, sizeof(struct element));
}
}
static void element_init(struct cc_ctrl_data *cc_data) {
unsigned int i;
for (i = 1; i < MAX_ELEMENTS; i++)
element_invalidate(cc_data, i);
}
static int element_set(struct cc_ctrl_data *cc_data, unsigned int id,
const uint8_t *data, uint32_t size) {
struct element *e;
e = element_get(cc_data, id);
if (e == NULL)
return 0;
/* check size */
if ((datatype_sizes[id] != 0) && (datatype_sizes[id] != size)) {
LOG("size %d of datatype_id %d doesn't match", size, id);
return 0;
}
free(e->data);
e->data = malloc(size);
memcpy(e->data, data, size);
e->size = size;
e->valid = 1;
LOGM("_element_set_ stored %d with len %d", id, size);
// hexdump("DATA: ", (void *)data, size);
return 1;
}
static int element_set_certificate(struct cc_ctrl_data *cc_data,
unsigned int id, X509 *cert) {
unsigned char *cert_der = NULL;
int cert_len;
cert_len = i2d_X509(cert, &cert_der);
if (cert_len <= 0) {
LOG("can not get data in DER format");
return 0;
}
if (!element_set(cc_data, id, cert_der, cert_len)) {
LOG("can not store element (%d)", id);
return 0;
}
return 1;
}
static int element_set_hostid_from_certificate(struct cc_ctrl_data *cc_data,
unsigned int id, X509 *cert) {
X509_NAME *subject;
int nid_cn = OBJ_txt2nid("CN");
char hostid[20];
uint8_t bin_hostid[8];
if ((id != 5) && (id != 6)) {
LOG("wrong datatype_id for hostid");
return 0;
}
subject = X509_get_subject_name(cert);
X509_NAME_get_text_by_NID(subject, nid_cn, hostid, sizeof(hostid));
if (strlen(hostid) != 16) {
LOG("malformed hostid");
return 0;
}
LOG("%sID: %s", id == 5 ? "HOST" : "CICAM", hostid);
str2bin(bin_hostid, hostid, 16);
if (!element_set(cc_data, id, bin_hostid, sizeof(bin_hostid))) {
LOG("can not set hostid");
return 0;
}
return 1;
}
static int element_valid(struct cc_ctrl_data *cc_data, unsigned int id) {
struct element *e;
e = element_get(cc_data, id);
return e && e->valid;
}
static unsigned int element_get_buf(struct cc_ctrl_data *cc_data, uint8_t *dest,
unsigned int id) {
struct element *e;
e = element_get(cc_data, id);
if (e == NULL)
return 0;
if (!e->valid) {
LOG("element_get_buf: datatype %d not valid", id);
return 0;
}
if (!e->data) {
LOG("element_get_buf: datatype %d doesn't exist", id);
return 0;
}
if (dest)
memcpy(dest, e->data, e->size);
return e->size;
}
static unsigned int element_get_req(struct cc_ctrl_data *cc_data, uint8_t *dest,
unsigned int id) {
unsigned int len = element_get_buf(cc_data, &dest[3], id);
if (len == 0) {
LOG("can not get element %d", id);
return 0;
}
dest[0] = id;
dest[1] = len >> 8;
dest[2] = len;
return 3 + len;
}
static uint8_t *element_get_ptr(struct cc_ctrl_data *cc_data, unsigned int id) {
struct element *e;
e = element_get(cc_data, id);
if (e == NULL)
return NULL;
if (!e->valid) {
LOG("element_get_ptr: datatype %u not valid", id);
return NULL;
}
if (!e->data) {
LOG("element_get_ptr: datatype %u doesn't exist", id);
return NULL;
}
return e->data;
}
void get_authdata_filename(char *dest, size_t len, unsigned int slot,
char *ci_name) {
char cin[128];
/* add module name to slot authorization bin file */
memset(cin, 0, sizeof(cin));
strncpy(cin, ci_name, sizeof(cin) - 1);
/* quickly replace blanks */
int i = 0;
while (cin[i] != 0) {
if (cin[i] == 32)
cin[i] = 95; /* underscore _ */
i++;
};
snprintf(dest, len, "%s/ci_auth_%s_%d.bin", opts.cache_dir, cin, slot);
}
static int get_authdata(uint8_t *host_id, uint8_t *dhsk, uint8_t *akh,
unsigned int slot, unsigned int index, char *ci_name) {
char filename[FILENAME_MAX];
int fd;
uint8_t chunk[8 + 256 + 32];
unsigned int i;
/* 5 pairs of data only */
if (index > MAX_PAIRS)
return 0;
get_authdata_filename(filename, sizeof(filename), slot, ci_name);
fd = open(filename, O_RDONLY);
if (fd < 0) {
LOG("cannot open %s", filename);
return 0;
}
for (i = 0; i < MAX_PAIRS; i++) {
if (read(fd, chunk, sizeof(chunk)) != sizeof(chunk)) {
LOG("cannot read auth_data");
close(fd);
return 0;
}
if (i == index) {
memcpy(host_id, chunk, 8);
memcpy(dhsk, &chunk[8], 256);
memcpy(akh, &chunk[8 + 256], 32);
close(fd);
return 1;
}
}
close(fd);
return 0;
}
static int write_authdata(unsigned int slot, const uint8_t *host_id,
const uint8_t *dhsk, const uint8_t *akh,
char *ci_name) {
char filename[FILENAME_MAX];
int fd;
uint8_t buf[(8 + 256 + 32) * MAX_PAIRS];
unsigned int entries;
unsigned int i;
int ret = 0;
for (entries = 0; entries < MAX_PAIRS; entries++) {
int offset = (8 + 256 + 32) * entries;
if (!get_authdata(&buf[offset], &buf[offset + 8],
&buf[offset + 8 + 256], slot, entries, ci_name))
break;
/* check if we got this pair already */
if (!memcmp(&buf[offset + 8 + 256], akh, 32)) {
LOG("data already stored");
return 1;
}
}
LOG("got %d entries", entries);
get_authdata_filename(filename, sizeof(filename), slot, ci_name);
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd < 0) {
LOG("cannot open %s for writing - authdata not stored", filename);
return 0;
}
/* store new entry first */
if (write(fd, host_id, 8) != 8) {
LOG("error in write");
goto end;
}
if (write(fd, dhsk, 256) != 256) {
LOG("error in write");
goto end;
}
if (write(fd, akh, 32) != 32) {
LOG("error in write");
goto end;
}
/* skip the last one if exists */
if (entries > 3)
entries = 3;
for (i = 0; i < entries; i++) {
int offset = (8 + 256 + 32) * i;
if (write(fd, &buf[offset], (8 + 256 + 32)) != (8 + 256 + 32)) {
LOG("error in write");
goto end;
}
}
ret = 1;
end:
close(fd);
return ret;
}
static int data_initialize(ca_device_t *d) {
uint8_t buf[32], host_id[8];
memset(&d->private_data, 0, sizeof(d->private_data));
/* clear storage of credentials */
element_init(&d->private_data);
/* set status field - OK */
memset(buf, 0, 1);
if (!element_set(&d->private_data, 30, buf, 1)) {
LOG("can not set status in elements");
}
/* set uri versions */
memset(buf, 0, 32);
buf[31] = d->uri_mask; // uri version bitmask, e.g. 1-3
LOG("uri version bitmask set to '%d'", buf[31]);
if (!element_set(&d->private_data, 29, buf, 32)) {
LOG("can not set uri_versions in elements");
}
/* load first AKH */
d->private_data.akh_index = 0;
if (!get_authdata(host_id, d->private_data.dhsk, buf, d->id,
d->private_data.akh_index, d->ci_name)) {
// if (!get_authdata(host_id, data->dhsk, buf, 1,
// data->akh_index)) {
/* no AKH available */
memset(buf, 0, sizeof(buf));
d->private_data.akh_index = 5; /* last one */
}
if (!element_set(&d->private_data, 22, buf, 32)) {
LOG("can not set AKH in elements");
}
if (!element_set(&d->private_data, 5, host_id, 8)) {
LOG("can not set host_id elements");
}
return 1;
}
/* content_control commands */
static int sac_check_auth(const uint8_t *data, unsigned int len, uint8_t *sak) {
struct aes_xcbc_mac_ctx ctx;
uint8_t calced_signature[16];
if (len < 16) {
LOG("auth too short");
return 0;
}
aes_xcbc_mac_init(&ctx, sak);
aes_xcbc_mac_process(&ctx, (uint8_t *)"\x04", 1); /* header len */
aes_xcbc_mac_process(&ctx, data, len - 16);
aes_xcbc_mac_done(&ctx, calced_signature);
if (memcmp(&data[len - 16], calced_signature, 16)) {
LOG("signature wrong");
return 0;
}
LOG("auth ok!");
return 1;
}
static int sac_gen_auth(uint8_t *out, uint8_t *in, unsigned int len,
uint8_t *sak) {
struct aes_xcbc_mac_ctx ctx;
aes_xcbc_mac_init(&ctx, sak);
aes_xcbc_mac_process(&ctx, (uint8_t *)"\x04", 1); /* header len */
aes_xcbc_mac_process(&ctx, in, len);
aes_xcbc_mac_done(&ctx, out);
return 16;
}
static void generate_key_seed(struct cc_ctrl_data *cc_data) {
/* this is triggered by new ns_module */
/* generate new key_seed -> SEK/SAK key derivation */
SHA256_CTX sha;
SHA256_Init(&sha);
SHA256_Update(&sha, &cc_data->dhsk[240], 16);
SHA256_Update(&sha, element_get_ptr(cc_data, 22),
element_get_buf(cc_data, NULL, 22));
SHA256_Update(&sha, element_get_ptr(cc_data, 20),
element_get_buf(cc_data, NULL, 20));
SHA256_Update(&sha, element_get_ptr(cc_data, 21),
element_get_buf(cc_data, NULL, 21));
SHA256_Final(cc_data->ks_host, &sha);
}
static void generate_ns_host(struct cc_ctrl_data *cc_data)
{
uint8_t buf[8];
get_random(buf, sizeof(buf));
element_set(cc_data, 20, buf, sizeof(buf));
}