Browse Source

Enable decryption through libdvbcsa

Georgi Chorbadzhiyski 13 years ago
parent
commit
81ebf20ce7
2 changed files with 46 additions and 5 deletions
  1. 1
    1
      libts
  2. 45
    4
      tsdecrypt.c

+ 1
- 1
libts

@@ -1 +1 @@
1
-Subproject commit d657ce22b0164873bbac231ada39e310cbedba10
1
+Subproject commit a0ebea40a64bcc888a995fcda7ad89a5c442d5d9

+ 45
- 4
tsdecrypt.c View File

@@ -18,13 +18,17 @@
18 18
 #include <openssl/aes.h>
19 19
 #include <openssl/md5.h>
20 20
 
21
+#include <dvbcsa/dvbcsa.h>
22
+
21 23
 #include "libfuncs/libfuncs.h"
22 24
 #include "libts/tsfuncs.h"
23 25
 
24 26
 #include "util.h"
25 27
 
26 28
 uint8_t cur_cw[16];
29
+int is_valid_cw = 0;
27 30
 uint8_t invalid_cw[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
31
+struct dvbcsa_key_s *csakey[2];
28 32
 
29 33
 static inline int valid_cw(uint8_t *cw) {
30 34
 	return memcmp(cw, invalid_cw, 16) != 0;
@@ -231,6 +235,10 @@ NEXT:
231 235
 	ts_hex_dump_buf(cw_dump, 16 * 6, cw, 16, 0);
232 236
 	ts_LOGf("CW  | CAID: 0x%04x ---------------------------------- IDX: 0x%04x Data: %s\n", ca_id, idx, cw_dump);
233 237
 
238
+	is_valid_cw = valid_cw(cur_cw);
239
+	dvbcsa_key_set(cur_cw    , csakey[0]);
240
+	dvbcsa_key_set(cur_cw + 8, csakey[1]);
241
+
234 242
 	return NULL;
235 243
 }
236 244
 
@@ -421,8 +429,8 @@ void process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
421 429
 	show_ts_pack(pid, !duplicate ? "ecm" : "ec+", NULL, ts_packet);
422 430
 }
423 431
 
424
-void ts_process_packets(struct ts *ts, uint8_t *data, uint8_t data_len) {
425
-	int i;
432
+void ts_process_packets(struct ts *ts, uint8_t *data, ssize_t data_len) {
433
+	ssize_t i;
426 434
 	for (i=0; i<data_len; i += 188) {
427 435
 		uint8_t *ts_packet = data + i;
428 436
 		uint16_t pid = ts_packet_get_pid(ts_packet);
@@ -437,10 +445,34 @@ void ts_process_packets(struct ts *ts, uint8_t *data, uint8_t data_len) {
437 445
 
438 446
 		if (!ts_pack_shown)
439 447
 			dump_ts_pack(pid, ts_packet);
448
+
449
+		int scramble_idx = ts_packet_get_scrambled(ts_packet);
450
+		if (scramble_idx > 1) {
451
+			if (is_valid_cw) {
452
+				// scramble_idx 2 == even key
453
+				// scramble_idx 3 == odd key
454
+				ts_packet_set_not_scrambled(ts_packet);
455
+				dvbcsa_decrypt(csakey[scramble_idx - 2], ts_packet + 4, 184);
456
+			}
457
+		}
458
+
440 459
 		ts_pack++;
441 460
 	}
442 461
 }
443 462
 
463
+void ts_write_packets(struct ts *ts, uint8_t *data, ssize_t data_len) {
464
+	ts = ts;
465
+	write(1, data, data_len);
466
+	return;
467
+/*
468
+	ssize_t i;
469
+	for (i=0; i<data_len; i += 188) {
470
+		uint8_t *ts_packet = data + i;
471
+		uint16_t pid = ts_packet_get_pid(ts_packet);
472
+		write(1, ts_packet, 188);
473
+	}
474
+*/
475
+}
444 476
 
445 477
 void show_help() {
446 478
 	printf("TSDECRYPT v1.0\n");
@@ -520,6 +552,9 @@ int main(int argc, char **argv) {
520 552
 	ssize_t readen;
521 553
 	uint8_t ts_packet[FRAME_SIZE];
522 554
 
555
+	csakey[0] = dvbcsa_key_alloc();
556
+	csakey[1] = dvbcsa_key_alloc();
557
+
523 558
 	memset(cur_cw, 0, sizeof(cur_cw));
524 559
 	ts_set_log_func(LOG_func);
525 560
 
@@ -528,10 +563,16 @@ int main(int argc, char **argv) {
528 563
 	struct ts *ts = ts_alloc();
529 564
 	do {
530 565
 		readen = read(0, ts_packet, FRAME_SIZE);
531
-		ts_process_packets(ts, ts_packet, readen);
532
-	} while (readen == FRAME_SIZE);
566
+		if (readen > 0) {
567
+			ts_process_packets(ts, ts_packet, readen);
568
+			ts_write_packets(ts, ts_packet, readen);
569
+		}
570
+	} while (readen > 0);
533 571
 	ts_free(&ts);
534 572
 
573
+	dvbcsa_key_free(csakey[0]);
574
+	dvbcsa_key_free(csakey[1]);
575
+
535 576
 	shutdown_fd(&server_fd);
536 577
 	exit(0);
537 578
 }

Loading…
Cancel
Save