-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-cccam.c
4550 lines (3959 loc) · 108 KB
/
module-cccam.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
#include "globals.h"
#ifdef MODULE_CCCAM
#include "cscrypt/md5.h"
#include "cscrypt/sha1.h"
#include "module-cacheex.h"
#include "module-cccam.h"
#include "module-cccam-data.h"
#include "module-cccshare.h"
#include "oscam-chk.h"
#include "oscam-cache.h"
#include "oscam-client.h"
#include "oscam-ecm.h"
#include "oscam-emm.h"
#include "oscam-failban.h"
#include "oscam-garbage.h"
#include "oscam-lock.h"
#include "oscam-net.h"
#include "oscam-reader.h"
#include "oscam-string.h"
#include "oscam-time.h"
#include "oscam-work.h"
//Mode names for CMD_05 command:
static const char *cmd05_mode_name[] = { "UNKNOWN", "PLAIN", "AES", "CC_CRYPT", "RC4",
"LEN=0"
};
//Mode names for CMD_0C command:
static const char *cmd0c_mode_name[] = { "NONE", "RC6", "RC4", "CC_CRYPT", "AES", "IDEA" };
uint8_t cc_node_id[8];
int32_t cc_cli_connect(struct s_client *cl);
int32_t cc_send_pending_emms(struct s_client *cl);
#define getprefix() (!cl?"":(!cl->cc?"":(((struct cc_data *)(cl->cc))->prefix)))
void cc_init_crypt(struct cc_crypt_block *block, uint8_t *key, int32_t len)
{
int32_t i = 0;
uint8_t j = 0;
for(i = 0; i < 256; i++)
{
block->keytable[i] = i;
}
for(i = 0; i < 256; i++)
{
j += key[i % len] + block->keytable[i];
SWAPC(&block->keytable[i], &block->keytable[j]);
}
block->state = *key;
block->counter = 0;
block->sum = 0;
}
void cc_crypt(struct cc_crypt_block *block, uint8_t *data, int32_t len,
cc_crypt_mode_t mode)
{
int32_t i;
uint8_t z;
for(i = 0; i < len; i++)
{
block->counter++;
block->sum += block->keytable[block->counter];
SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
z = data[i];
data[i] = z ^ block->keytable[(block->keytable[block->counter]
+ block->keytable[block->sum]) & 0xff];
data[i] ^= block->state;
if(!mode)
{ z = data[i]; }
block->state = block->state ^ z;
}
}
void cc_rc4_crypt(struct cc_crypt_block *block, uint8_t *data, int32_t len,
cc_crypt_mode_t mode)
{
int32_t i;
uint8_t z;
for(i = 0; i < len; i++)
{
block->counter++;
block->sum += block->keytable[block->counter];
SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
z = data[i];
data[i] = z ^ block->keytable[(block->keytable[block->counter]
+ block->keytable[block->sum]) & 0xff];
if(!mode)
{ z = data[i]; }
block->state = block->state ^ z;
}
}
void cc_xor(uint8_t *buf)
{
const char cccam[] = "CCcam";
uint8_t i;
for(i = 0; i < 8; i++)
{
buf[8 + i] = i * buf[i];
if(i <= 5)
{
buf[i] ^= cccam[i];
}
}
}
void cc_cw_crypt(struct s_client *cl, uint8_t *cws, uint32_t cardid)
{
struct cc_data *cc = cl->cc;
int64_t node_id;
uint8_t tmp;
int32_t i;
if(cl->typ != 'c')
{
node_id = b2ll(8, cc->node_id);
}
else
{
node_id = b2ll(8, cc->peer_node_id);
}
for(i = 0; i < 16; i++)
{
tmp = cws[i] ^(node_id >> (4 * i));
if(i & 1)
{ tmp = ~tmp; }
cws[i] = (cardid >> (2 * i)) ^ tmp;
}
}
/** swap endianness (int) */
static void SwapLBi(unsigned char *buff, int32_t len)
{
#if __BYTE_ORDER != __BIG_ENDIAN
return;
#endif
int32_t i;
unsigned char swap[4];
for(i = 0; i < len / 4; i++)
{
memcpy(swap, buff, 4);
buff[0] = swap[3];
buff[1] = swap[2];
buff[2] = swap[1];
buff[3] = swap[0];
buff += 4;
}
}
void cc_crypt_cmd0c(struct s_client *cl, uint8_t *buf, int32_t len)
{
struct cc_data *cc = cl->cc;
uint8_t *out;
if(!cs_malloc(&out, len))
{ return; }
switch(cc->cmd0c_mode)
{
case MODE_CMD_0x0C_NONE: // none additional encryption
{
memcpy(out, buf, len);
break;
}
case MODE_CMD_0x0C_RC6 : //RC6
{
// buf may be unaligned,
// so we use malloc() memory for the uint32_t* cast
uint8_t *tmp;
int32_t i;
if(!cs_malloc(&tmp, len))
{ return; }
memcpy(tmp, buf, len);
SwapLBi(tmp, len);
for(i = 0; i < len / 16; i++)
{ rc6_block_decrypt((uint32_t *)(tmp + i * 16), (uint32_t *)(out + i * 16), 1, cc->cmd0c_RC6_cryptkey); }
SwapLBi(out, len);
NULLFREE(tmp);
break;
}
case MODE_CMD_0x0C_RC4: // RC4
{
cc_rc4_crypt(&cc->cmd0c_cryptkey, buf, len, ENCRYPT);
memcpy(out, buf, len);
break;
}
case MODE_CMD_0x0C_CC_CRYPT: // cc_crypt
{
cc_crypt(&cc->cmd0c_cryptkey, buf, len, DECRYPT);
memcpy(out, buf, len);
break;
}
case MODE_CMD_0x0C_AES: // AES
{
int32_t i;
for(i = 0; i < len / 16; i++)
AES_decrypt((unsigned char *) buf + i * 16,
(unsigned char *) out + i * 16, &cc->cmd0c_AES_key);
break;
}
case MODE_CMD_0x0C_IDEA : //IDEA
{
int32_t i = 0;
int32_t j;
while(i < len)
{
idea_ecb_encrypt(buf + i, out + i, &cc->cmd0c_IDEA_dkey);
i += 8;
}
i = 8;
while(i < len)
{
for(j = 0; j < 8; j++)
{ out[j + i] ^= buf[j + i - 8]; }
i += 8;
}
break;
}
}
memcpy(buf, out, len);
NULLFREE(out);
}
void set_cmd0c_cryptkey(struct s_client *cl, uint8_t *key, uint8_t len)
{
struct cc_data *cc = cl->cc;
uint8_t key_buf[32];
memset(&key_buf, 0, sizeof(key_buf));
if(len > 32)
{ len = 32; }
memcpy(key_buf, key, len);
switch(cc->cmd0c_mode)
{
case MODE_CMD_0x0C_NONE : //NONE
{
break;
}
case MODE_CMD_0x0C_RC6 : //RC6
{
rc6_key_setup(key_buf, 32, cc->cmd0c_RC6_cryptkey);
break;
}
case MODE_CMD_0x0C_RC4: //RC4
case MODE_CMD_0x0C_CC_CRYPT: //CC_CRYPT
{
cc_init_crypt(&cc->cmd0c_cryptkey, key_buf, 32);
break;
}
case MODE_CMD_0x0C_AES: //AES
{
memset(&cc->cmd0c_AES_key, 0, sizeof(cc->cmd0c_AES_key));
AES_set_decrypt_key((unsigned char *) key_buf, 256, &cc->cmd0c_AES_key);
break;
}
case MODE_CMD_0x0C_IDEA : //IDEA
{
uint8_t key_buf_idea[16];
memcpy(key_buf_idea, key_buf, 16);
IDEA_KEY_SCHEDULE ekey;
idea_set_encrypt_key(key_buf_idea, &ekey);
idea_set_decrypt_key(&ekey, &cc->cmd0c_IDEA_dkey);
break;
}
}
}
int32_t sid_eq(struct cc_srvid *srvid1, struct cc_srvid *srvid2)
{
return (srvid1->sid == srvid2->sid && (srvid1->chid == srvid2->chid || !srvid1->chid || !srvid2->chid) && (srvid1->ecmlen == srvid2->ecmlen || !srvid1->ecmlen || !srvid2->ecmlen));
}
int32_t sid_eq_nb(struct cc_srvid *srvid1, struct cc_srvid_block *srvid2)
{
return sid_eq(srvid1, (struct cc_srvid *)srvid2);
}
int32_t sid_eq_bb(struct cc_srvid_block *srvid1, struct cc_srvid_block *srvid2)
{
return (srvid1->sid == srvid2->sid && (srvid1->chid == srvid2->chid || !srvid1->chid || !srvid2->chid) && (srvid1->ecmlen == srvid2->ecmlen || !srvid1->ecmlen || !srvid2->ecmlen)
&& (srvid1->blocked_till == srvid2->blocked_till || !srvid1->blocked_till || !srvid2->blocked_till));
}
struct cc_srvid_block *is_sid_blocked(struct cc_card *card, struct cc_srvid *srvid_blocked)
{
LL_ITER it = ll_iter_create(card->badsids);
struct cc_srvid_block *srvid;
while((srvid = ll_iter_next(&it)))
{
if(sid_eq_nb(srvid_blocked, srvid))
{
break;
}
}
return srvid;
}
uint32_t has_perm_blocked_sid(struct cc_card *card)
{
LL_ITER it = ll_iter_create(card->badsids);
struct cc_srvid_block *srvid;
while((srvid = ll_iter_next(&it)))
{
if(srvid->blocked_till == 0)
{
break;
}
}
return srvid != NULL;
}
struct cc_srvid *is_good_sid(struct cc_card *card, struct cc_srvid *srvid_good)
{
LL_ITER it = ll_iter_create(card->goodsids);
struct cc_srvid *srvid;
while((srvid = ll_iter_next(&it)))
{
if(sid_eq(srvid, srvid_good))
{
break;
}
}
return srvid;
}
#define BLOCKING_SECONDS 60
void add_sid_block(struct s_client *cl __attribute__((unused)), struct cc_card *card,
struct cc_srvid *srvid_blocked)
{
if(is_sid_blocked(card, srvid_blocked))
{ return; }
struct cc_srvid_block *srvid;
if(!cs_malloc(&srvid, sizeof(struct cc_srvid_block)))
{ return; }
memcpy(srvid, srvid_blocked, sizeof(struct cc_srvid));
srvid->blocked_till = time(NULL) + BLOCKING_SECONDS;
ll_append(card->badsids, srvid);
cs_debug_mask(D_READER, "%s added sid block %04X(CHID %04X, length %d) for card %08x",
getprefix(), srvid_blocked->sid, srvid_blocked->chid, srvid_blocked->ecmlen,
card->id);
}
void remove_sid_block(struct cc_card *card, struct cc_srvid *srvid_blocked)
{
LL_ITER it = ll_iter_create(card->badsids);
struct cc_srvid_block *srvid;
while((srvid = ll_iter_next(&it)))
if(sid_eq_nb(srvid_blocked, srvid))
{ ll_iter_remove_data(&it); }
}
void remove_good_sid(struct cc_card *card, struct cc_srvid *srvid_good)
{
LL_ITER it = ll_iter_create(card->goodsids);
struct cc_srvid *srvid;
while((srvid = ll_iter_next(&it)))
if(sid_eq(srvid, srvid_good))
{ ll_iter_remove_data(&it); }
}
void add_good_sid(struct s_client *cl __attribute__((unused)), struct cc_card *card,
struct cc_srvid *srvid_good)
{
if(is_good_sid(card, srvid_good))
{ return; }
remove_sid_block(card, srvid_good);
struct cc_srvid *srvid;
if(!cs_malloc(&srvid, sizeof(struct cc_srvid)))
{ return; }
memcpy(srvid, srvid_good, sizeof(struct cc_srvid));
ll_append(card->goodsids, srvid);
cs_debug_mask(D_READER, "%s added good sid %04X(%d) for card %08x",
getprefix(), srvid_good->sid, srvid_good->ecmlen, card->id);
}
/**
* reader
* clears and frees values for reinit
*/
void cc_cli_close(struct s_client *cl, int32_t call_conclose)
{
struct s_reader *rdr = cl->reader;
struct cc_data *cc = cl->cc;
if(!rdr || !cc)
{ return; }
if(rdr) { rdr->tcp_connected = 0; }
if(rdr) { rdr->card_status = NO_CARD; }
if(rdr) { rdr->last_s = rdr->last_g = 0; }
if(cl) { cl->last = 0; }
if(call_conclose) //clears also pending ecms!
{ network_tcp_connection_close(rdr, "close"); }
else
{
if(cl->udp_fd)
{
close(cl->udp_fd);
cl->udp_fd = 0;
cl->pfd = 0;
}
}
cc->ecm_busy = 0;
cc->just_logged_in = 0;
}
struct cc_extended_ecm_idx *add_extended_ecm_idx(struct s_client *cl,
uint8_t send_idx, uint16_t ecm_idx, struct cc_card *card,
struct cc_srvid srvid, int8_t free_card)
{
struct cc_data *cc = cl->cc;
struct cc_extended_ecm_idx *eei;
if(!cs_malloc(&eei, sizeof(struct cc_extended_ecm_idx)))
{ return NULL; }
eei->send_idx = send_idx;
eei->ecm_idx = ecm_idx;
eei->card = card;
eei->cccam_id = card->id;
eei->srvid = srvid;
eei->free_card = free_card;
cs_ftime(&eei->tps);
ll_append(cc->extended_ecm_idx, eei);
//cs_debug_mask(D_TRACE, "%s add extended ecm-idx: %d:%d", getprefix(), send_idx, ecm_idx);
return eei;
}
struct cc_extended_ecm_idx *get_extended_ecm_idx(struct s_client *cl,
uint8_t send_idx, int32_t remove_item)
{
struct cc_data *cc = cl->cc;
struct cc_extended_ecm_idx *eei;
LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
while((eei = ll_iter_next(&it)))
{
if(eei->send_idx == send_idx)
{
if(remove_item)
{ ll_iter_remove(&it); }
//cs_debug_mask(D_TRACE, "%s get by send-idx: %d FOUND: %d",
// getprefix(), send_idx, eei->ecm_idx);
return eei;
}
}
#ifdef WITH_DEBUG
if(remove_item)
cs_debug_mask(cl->typ == 'c' ? D_CLIENT : D_READER, "%s get by send-idx: %d NOT FOUND", getprefix(),
send_idx);
#endif
return NULL;
}
struct cc_extended_ecm_idx *get_extended_ecm_idx_by_idx(struct s_client *cl,
uint16_t ecm_idx, int32_t remove_item)
{
struct cc_data *cc = cl->cc;
struct cc_extended_ecm_idx *eei;
LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
while((eei = ll_iter_next(&it)))
{
if(eei->ecm_idx == ecm_idx)
{
if(remove_item)
{ ll_iter_remove(&it); }
//cs_debug_mask(D_TRACE, "%s get by ecm-idx: %d FOUND: %d",
// getprefix(), ecm_idx, eei->send_idx);
return eei;
}
}
#ifdef WITH_DEBUG
if(remove_item)
cs_debug_mask(cl->typ == 'c' ? D_CLIENT : D_READER, "%s get by ecm-idx: %d NOT FOUND", getprefix(),
ecm_idx);
#endif
return NULL;
}
void cc_reset_pending(struct s_client *cl, int32_t ecm_idx)
{
int32_t i = 0;
for(i = 0; i < cfg.max_pending; i++)
{
if(cl->ecmtask[i].idx == ecm_idx && cl->ecmtask[i].rc == E_ALREADY_SENT)
{ cl->ecmtask[i].rc = E_UNHANDLED; } //Mark unused
}
}
void free_extended_ecm_idx_by_card(struct s_client *cl, struct cc_card *card, int8_t null_only)
{
struct cc_data *cc = cl->cc;
struct cc_extended_ecm_idx *eei;
LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
while((eei = ll_iter_next(&it)))
{
if(eei->card == card)
{
if(null_only)
{
cc_reset_pending(cl, eei->ecm_idx);
if(eei->free_card)
{ NULLFREE(eei->card); }
ll_iter_remove_data(&it);
}
else
{
if(eei->free_card)
{ NULLFREE(eei->card); }
eei->card = NULL;
}
}
}
}
void free_extended_ecm_idx(struct cc_data *cc)
{
struct cc_extended_ecm_idx *eei;
LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
while((eei = ll_iter_next(&it)))
{
if(eei->free_card)
{ NULLFREE(eei->card); }
ll_iter_remove_data(&it);
}
}
int32_t cc_recv_to(struct s_client *cl, uint8_t *buf, int32_t len)
{
int32_t rc;
struct pollfd pfd;
while(1)
{
pfd.fd = cl->udp_fd;
pfd.events = POLLIN | POLLPRI;
rc = poll(&pfd, 1, cfg.cc_recv_timeout);
if(rc < 0)
{
if(errno == EINTR) { continue; }
return (-1); //error!!
}
if(rc == 1)
{
if(pfd.revents & POLLHUP)
{ return (-1); } //hangup = error!!
else
{ break; }
}
else
{ return (-2); } //timeout!!
}
return recv(cl->udp_fd, buf, len, MSG_WAITALL);
}
/**
* reader
* closes the connection and reopens it.
*/
static int8_t cc_cycle_connection(struct s_client *cl)
{
if(!cl || cl->kill)
{ return 0; }
cs_debug_mask(D_TRACE, "%s unlocked-cycleconnection! timeout %d ms",
getprefix(), cl->reader->cc_reconnect);
cc_cli_close(cl, 0);
cs_sleepms(50);
cc_cli_connect(cl);
return cl->reader->tcp_connected;
}
/**
* reader+server:
* receive a message
*/
int32_t cc_msg_recv(struct s_client *cl, uint8_t *buf, int32_t maxlen)
{
struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
int32_t len;
struct cc_data *cc = cl->cc;
int32_t handle = cl->udp_fd;
if(handle <= 0 || maxlen < 4)
{ return -1; }
if(!cl->cc) { return -1; }
cs_writelock(&cc->lockcmd);
if(!cl->cc)
{
cs_writeunlock(&cc->lockcmd);
return -1;
}
len = recv(handle, buf, 4, MSG_WAITALL);
if(len != 4) // invalid header length read
{
if(len <= 0)
{ cs_debug_mask(cl->typ == 'c' ? D_CLIENT : D_READER, "%s disconnected by remote server", getprefix()); }
else
{ cs_debug_mask(cl->typ == 'c' ? D_CLIENT : D_READER, "%s invalid header length (expected 4, read %d)", getprefix(), len); }
cs_writeunlock(&cc->lockcmd);
return -1;
}
cc_crypt(&cc->block[DECRYPT], buf, 4, DECRYPT);
//cs_ddump_mask(D_CLIENT, buf, 4, "cccam: decrypted header:");
cc->g_flag = buf[0];
int32_t size = (buf[2] << 8) | buf[3];
if(size) // check if any data is expected in msg
{
if(size > maxlen)
{
cs_writeunlock(&cc->lockcmd);
cs_debug_mask(cl->typ == 'c' ? D_CLIENT : D_READER, "%s message too big (size=%d max=%d)", getprefix(), size, maxlen);
return 0;
}
len = recv(handle, buf + 4, size, MSG_WAITALL);
if(rdr && buf[1] == MSG_CW_ECM)
{ rdr->last_g = time(NULL); }
if(len != size)
{
cs_writeunlock(&cc->lockcmd);
if(len <= 0)
{ cs_debug_mask(cl->typ == 'c' ? D_CLIENT : D_READER, "%s disconnected by remote", getprefix()); }
else
cs_debug_mask(cl->typ == 'c' ? D_CLIENT : D_READER, "%s invalid message length read (expected %d, read %d)",
getprefix(), size, len);
return -1;
}
cc_crypt(&cc->block[DECRYPT], buf + 4, len, DECRYPT);
len += 4;
}
cs_writeunlock(&cc->lockcmd);
//cs_ddump_mask(cl->typ=='c'?D_CLIENT:D_READER, buf, len, "cccam: full decrypted msg, len=%d:", len);
return len;
}
/**
* reader+server
* send a message
*/
int32_t cc_cmd_send(struct s_client *cl, uint8_t *buf, int32_t len, cc_msg_type_t cmd)
{
if(!cl->udp_fd) //disconnected
{ return -1; }
struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
int32_t n;
struct cc_data *cc = cl->cc;
if(!cl->cc || cl->kill) { return -1; }
cs_writelock(&cc->lockcmd);
if(!cl->cc || cl->kill)
{
cs_writeunlock(&cc->lockcmd);
return -1;
}
uint8_t *netbuf;
if(!cs_malloc(&netbuf, len + 4))
{ return -1; }
if(cmd == MSG_NO_HEADER)
{
memcpy(netbuf, buf, len);
}
else
{
// build command message
netbuf[0] = cc->g_flag; // flags??
netbuf[1] = cmd & 0xff;
netbuf[2] = len >> 8;
netbuf[3] = len & 0xff;
if(buf)
{ memcpy(netbuf + 4, buf, len); }
len += 4;
}
//cs_ddump_mask(D_CLIENT, netbuf, len, "cccam: send:");
cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT);
n = send(cl->udp_fd, netbuf, len, 0);
if(rdr) { rdr->last_s = time(NULL); }
if(cl) { cl->last = time(NULL); }
cs_writeunlock(&cc->lockcmd);
NULLFREE(netbuf);
if(n != len)
{
if(rdr)
{ cc_cli_close(cl, 1); }
else
{
cs_writeunlock(&cc->cards_busy);
cs_disconnect_client(cl);
}
n = -1;
}
return n;
}
#define CC_DEFAULT_VERSION 1
#define CC_VERSIONS 8
static char *version[CC_VERSIONS] = { "2.0.11", "2.1.1", "2.1.2", "2.1.3", "2.1.4", "2.2.0", "2.2.1", "2.3.0"};
static char *build[CC_VERSIONS] = { "2892", "2971", "3094", "3165", "3191", "3290", "3316", "3367"};
static char extcompat[CC_VERSIONS] = { 0, 0, 0, 0, 0, 1, 1, 1}; //Supporting new card format starting with 2.2.0
/**
* reader+server
* checks the cccam-version in the configuration
*/
void cc_check_version(char *cc_version, char *cc_build)
{
int32_t i;
for(i = 0; i < CC_VERSIONS; i++)
{
if(!memcmp(cc_version, version[i], strlen(version[i])))
{
memcpy(cc_build, build[i], strlen(build[i]) + 1);
cs_debug_mask(D_CLIENT, "cccam: auto build set for version: %s build: %s",
cc_version, cc_build);
return;
}
}
memcpy(cc_version, version[CC_DEFAULT_VERSION], strlen(
version[CC_DEFAULT_VERSION]));
memcpy(cc_build, build[CC_DEFAULT_VERSION], strlen(
build[CC_DEFAULT_VERSION]));
cs_debug_mask(D_CLIENT, "cccam: auto version set: %s build: %s", cc_version, cc_build);
return;
}
int32_t check_cccam_compat(struct cc_data *cc)
{
int32_t res = 0;
int32_t i = 0;
for(i = 0; i < CC_VERSIONS; i++)
{
if(!strcmp(cfg.cc_version, version[i]))
{
res += extcompat[i];
break;
}
}
if(!res)
{ return 0; }
for(i = 0; i < CC_VERSIONS; i++)
{
if(!strcmp(cc->remote_version, version[i]))
{
res += extcompat[i];
break;
}
}
return res == 2;
}
/**
* reader
* sends own version information to the CCCam server
*/
int32_t cc_send_cli_data(struct s_client *cl)
{
struct s_reader *rdr = cl->reader;
struct cc_data *cc = cl->cc;
const int32_t size = 20 + 8 + 6 + 26 + 4 + 28 + 1;
uint8_t buf[size];
cs_debug_mask(D_READER, "cccam: send client data");
memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
memcpy(buf, rdr->r_usr, sizeof(rdr->r_usr));
memcpy(buf + 20, cc->node_id, 8);
buf[28] = rdr->cc_want_emu; // <-- Client want to have EMUs, 0 - NO; 1 - YES
memcpy(buf + 29, rdr->cc_version, sizeof(rdr->cc_version)); // cccam version (ascii)
memcpy(buf + 61, rdr->cc_build, sizeof(rdr->cc_build)); // build number (ascii)
cs_debug_mask(D_READER, "%s sending own version: %s, build: %s", getprefix(),
rdr->cc_version, rdr->cc_build);
return cc_cmd_send(cl, buf, size, MSG_CLI_DATA);
}
/**
* server
* sends version information to the client
*/
int32_t cc_send_srv_data(struct s_client *cl)
{
struct cc_data *cc = cl->cc;
cs_debug_mask(D_CLIENT, "cccam: send server data");
memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
uint8_t buf[0x48];
memset(buf, 0, 0x48);
int32_t stealth = cl->account->cccstealth;
if(stealth == -1)
{ stealth = cfg.cc_stealth; }
if(stealth) { cc->node_id[7]++; }
memcpy(buf, cc->node_id, 8);
char cc_build[7], tmp_dbg[17];
memset(cc_build, 0, sizeof(cc_build));
cc_check_version((char *) cfg.cc_version, cc_build);
memcpy(buf + 8, cfg.cc_version, sizeof(cfg.cc_version)); // cccam version (ascii)
memcpy(buf + 40, cc_build, sizeof(cc_build)); // build number (ascii)
cs_debug_mask(D_CLIENT, "%s version: %s, build: %s nodeid: %s", getprefix(),
cfg.cc_version, cc_build, cs_hexdump(0, cc->peer_node_id, 8, tmp_dbg, sizeof(tmp_dbg)));
return cc_cmd_send(cl, buf, 0x48, MSG_SRV_DATA);
}
int32_t loop_check(uint8_t *myid, struct s_client *cl)
{
if(!cl)
{ return 0; }
struct cc_data *cc = cl->cc;
if(!cc)
{ return 0; }
return !memcmp(myid, cc->peer_node_id, sizeof(cc->peer_node_id)); // same nodeid? ignore
}
/**
* reader
* retrieves the next waiting ecm request
*/
int32_t cc_get_nxt_ecm(struct s_client *cl)
{
struct cc_data *cc = cl->cc;
ECM_REQUEST *er, *ern = NULL;
int32_t n, i, pending = 0;
struct timeb t;
cs_ftime(&t);
int32_t diff = (int32_t)cfg.ctimeout + 500;
n = -1;
for(i = 0; i < cfg.max_pending; i++)
{
er = &cl->ecmtask[i];
if((comp_timeb(&t, &er->tps) >= diff) && (er->rc >= E_NOCARD)) // drop timeouts
{
write_ecm_answer(cl->reader, er, E_TIMEOUT, 0, NULL, NULL);
}
else if(er->rc >= E_NOCARD && er->rc <= E_UNHANDLED) // stil active and waiting
{
pending++;
if(loop_check(cc->peer_node_id, er->client))
{
cs_debug_mask(D_READER, "%s ecm loop detected! client %s (%8lX)",
getprefix(), er->client->account->usr, (unsigned long)er->client->thread);
write_ecm_answer(cl->reader, er, E_NOTFOUND, E2_CCCAM_LOOP, NULL, NULL);
}
else
// search for the ecm with the lowest time, this should be the next to go
if(n < 0 || (ern->tps.time - er->tps.time < 0))
{
//check for already pending:
if(cc && cc->extended_mode)
{
int32_t j, found;
ECM_REQUEST *erx;
for(found = j = 0; j < cfg.max_pending; j++)
{
erx = &cl->ecmtask[j];
if(i != j && erx->rc == E_ALREADY_SENT &&
er->caid == erx->caid &&
er->ecmd5 == erx->ecmd5)
{
found = 1;
break;
}
}
if(!found)
{
n = i;
ern = er;
}
}
else
{
n = i;
ern = er;
}
}
}
}
cl->pending = pending;
return n;
}
/**
* sends the secret cmd05 answer to the server
*/
int32_t send_cmd05_answer(struct s_client *cl)
{
struct cc_data *cc = cl->cc;
if(!cc->cmd05_active || cc->ecm_busy) //exit if not in cmd05 or waiting for ECM answer
{ return 0; }
cc->cmd05_active--;
if(cc->cmd05_active)
{ return 0; }
uint8_t *data = cc->cmd05_data;
cc_cmd05_mode cmd05_mode = MODE_UNKNOWN;
// by Project:Keynation
switch(cc->cmd05_data_len)
{
case 0: //payload 0, return with payload 0!
{
cc_cmd_send(cl, NULL, 0, MSG_CMD_05);
cmd05_mode = MODE_LEN0;
break;
}
case 256:
{
cmd05_mode = cc->cmd05_mode;
switch(cmd05_mode)
{
case MODE_PLAIN: //Send plain unencrypted back
{
cc_cmd_send(cl, data, 256, MSG_CMD_05);
break;
}
case MODE_AES: //encrypt with received aes128 key:
{
AES_KEY key;
uint8_t aeskey[16];
uint8_t out[256];
memcpy(aeskey, cc->cmd05_aeskey, 16);
memset(&key, 0, sizeof(key));
AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
int32_t i;