Browse Source

Initial import

Georgi Chorbadzhiyski 13 years ago
commit
5511ed71b6
6 changed files with 565 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 6
    0
      .gitmodules
  3. 47
    0
      Makefile
  4. 1
    0
      libfuncs
  5. 1
    0
      libts
  6. 508
    0
      tsdecrypt.c

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+*.o
2
+tsdecrypt

+ 6
- 0
.gitmodules View File

@@ -0,0 +1,6 @@
1
+[submodule "libts"]
2
+	path = libts
3
+	url = git@git:libts.git
4
+[submodule "libfuncs"]
5
+	path = libfuncs
6
+	url = git@git:libfuncs.git

+ 47
- 0
Makefile View File

@@ -0,0 +1,47 @@
1
+CC = $(CROSS)$(TARGET)gcc
2
+STRIP = $(CROSS)$(TARGET)strip
3
+CFLAGS = -ggdb -Wall -Wextra -Wshadow -Wformat-security -Wno-strict-aliasing -O2 -D_GNU_SOURCE
4
+RM = /bin/rm -f
5
+Q = @
6
+
7
+FUNCS_DIR = libfuncs
8
+FUNCS_LIB = $(FUNCS_DIR)/libfuncs.a
9
+
10
+TS_DIR = libts
11
+TS_LIB = $(TS_DIR)/libts.a
12
+
13
+tsdecrypt_OBJS = tsdecrypt.o $(FUNCS_LIB) $(TS_LIB)
14
+tsdecrypt_LIBS = -lcrypto -ldvbcsa -lpthread
15
+
16
+CLEAN_OBJS = tsdecrypt $(tsdecrypt_OBJS) *~
17
+
18
+PROGS = tsdecrypt
19
+all: $(PROGS)
20
+
21
+$(FUNCS_LIB):
22
+	$(Q)echo "  MAKE	$(FUNCS_LIB)"
23
+	$(Q)$(MAKE) -s -C $(FUNCS_DIR)
24
+
25
+$(TS_LIB):
26
+	$(Q)echo "  MAKE	$(TS_LIB)"
27
+	$(Q)$(MAKE) -s -C $(TS_DIR)
28
+
29
+tsdecrypt: $(tsdecrypt_OBJS)
30
+	$(Q)echo "  LINK	tsdecrypt"
31
+	$(Q)$(CC) $(CFLAGS) $(tsdecrypt_OBJS) $(tsdecrypt_LIBS) -o tsdecrypt
32
+
33
+%.o: %.c
34
+	$(Q)echo "  CC	tsdecrypt	$<"
35
+	$(Q)$(CC) $(CFLAGS)  -c $<
36
+
37
+strip:
38
+	$(Q)echo "  STRIP	$(PROGS)"
39
+	$(Q)$(STRIP) $(PROGS)
40
+
41
+clean:
42
+	$(Q)echo "  RM	$(CLEAN_OBJS)"
43
+	$(Q)$(RM) $(CLEAN_OBJS)
44
+
45
+distclean: clean
46
+	$(Q)$(MAKE) -s -C $(TS_DIR) clean
47
+	$(Q)$(MAKE) -s -C $(FUNCS_DIR) clean

+ 1
- 0
libfuncs

@@ -0,0 +1 @@
1
+Subproject commit 1ce770eae5110adb8a715d1688bbda51735d42bd

+ 1
- 0
libts

@@ -0,0 +1 @@
1
+Subproject commit 89d59c93e4ddb7fbf37d2c8a55a3aab4b589c77d

+ 508
- 0
tsdecrypt.c View File

@@ -0,0 +1,508 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include <unistd.h>
4
+#include <netdb.h>
5
+#include <sched.h>
6
+#include <string.h>
7
+#include <time.h>
8
+#include <fcntl.h>
9
+#include <sys/types.h>
10
+#include <sys/stat.h>
11
+#include <sys/time.h>
12
+#include <sys/errno.h>
13
+
14
+#include <openssl/aes.h>
15
+#include <openssl/md5.h>
16
+
17
+#include "libts/tsfuncs.h"
18
+
19
+enum CA_system req_CA_sys = CA_CONNAX;
20
+char *camd35_user = "user";
21
+char *camd35_pass = "pass";
22
+uint32_t camd35_auth = 0;
23
+AES_KEY camd35_aes_encrypt_key;
24
+AES_KEY camd35_aes_decrypt_key;
25
+
26
+void show_help() {
27
+	printf("TSCRYPT v1.0\n");
28
+	puts("Copyright (c) 2011 Unix Solutions Ltd.");
29
+	puts("");
30
+	puts("	Usage: tsdecrypt [opts] < data > data.decrypted");
31
+	puts("");
32
+	exit(0);
33
+}
34
+
35
+void parse_options(int argc, char **argv) {
36
+	int j;
37
+	while ((j = getopt(argc, argv, "f:h")) != -1) {
38
+		switch (j) {
39
+			case 'h':
40
+				show_help();
41
+				exit(0);
42
+		}
43
+	}
44
+}
45
+
46
+enum e_flag {
47
+	TYPE_EMM,
48
+	TYPE_ECM
49
+};
50
+
51
+void savefile(uint8_t *data, int datasize, enum e_flag flag) {
52
+	static int cnt = 0;
53
+	char *fname;
54
+	asprintf(&fname, "%03d-%s.dump", ++cnt, flag == TYPE_EMM ? "emm" : "ecm");
55
+	int fd = open(fname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
56
+	if (fd < 0) {
57
+		perror("open");
58
+		exit(1);
59
+	}
60
+	write(fd, data, datasize);
61
+	close(fd);
62
+	free(fname);
63
+}
64
+
65
+static unsigned long crc_table[256] = {
66
+  0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
67
+  0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
68
+  0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
69
+  0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
70
+  0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
71
+  0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
72
+  0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
73
+  0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
74
+  0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
75
+  0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
76
+  0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
77
+  0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
78
+  0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
79
+  0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
80
+  0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
81
+  0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
82
+  0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
83
+  0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
84
+  0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
85
+  0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
86
+  0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
87
+  0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
88
+  0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
89
+  0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
90
+  0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
91
+  0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
92
+  0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
93
+  0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
94
+  0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
95
+  0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
96
+  0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
97
+  0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
98
+  0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
99
+  0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
100
+  0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
101
+  0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
102
+  0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
103
+  0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
104
+  0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
105
+  0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
106
+  0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
107
+  0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
108
+  0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
109
+  0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
110
+  0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
111
+  0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
112
+  0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
113
+  0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
114
+  0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
115
+  0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
116
+  0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
117
+  0x2d02ef8dL
118
+};
119
+
120
+#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
121
+#define DO2(buf) DO1(buf); DO1(buf);
122
+#define DO4(buf) DO2(buf); DO2(buf);
123
+#define DO8(buf) DO4(buf); DO4(buf);
124
+
125
+unsigned long crc32(unsigned long crc, const uint8_t *buf, unsigned int len) {
126
+	if (!buf)
127
+		return 0L;
128
+	crc = crc ^ 0xffffffffL;
129
+	while (len >= 8) {
130
+		DO8(buf);
131
+		len -= 8;
132
+	}
133
+	if (len) {
134
+		do {
135
+			DO1(buf);
136
+		} while (--len);
137
+	}
138
+	return crc ^ 0xffffffffL;
139
+}
140
+
141
+int32_t boundary(int32_t exp, int32_t n) {
142
+	return ((((n-1) >> exp) + 1) << exp);
143
+}
144
+
145
+uint8_t *init_4b(uint32_t val, uint8_t *b) {
146
+	b[0] = (val >> 24) & 0xff;
147
+	b[1] = (val >> 16) & 0xff;
148
+	b[2] = (val >>  8) & 0xff;
149
+	b[3] = (val      ) & 0xff;
150
+	return b;
151
+}
152
+
153
+uint8_t *init_4l(uint32_t val, uint8_t *b) {
154
+	b[3] = (val >> 24) & 0xff;
155
+	b[2] = (val >> 16) & 0xff;
156
+	b[1] = (val >>  8) & 0xff;
157
+	b[0] = (val      ) & 0xff;
158
+	return b;
159
+}
160
+
161
+uint8_t *init_2b(uint32_t val, uint8_t *b) {
162
+	b[0] = (val >> 8) & 0xff;
163
+	b[1] = (val     ) & 0xff;
164
+	return b;
165
+}
166
+
167
+// 4 auth header, 20 header size, 256 max data size, 16 potential padding
168
+#define HDR_LEN     (20)
169
+#define BUF_SIZE	(4 + HDR_LEN + 256 + 16)
170
+
171
+static void camd35_init_auth(char *user, char *pass) {
172
+	unsigned char dump[16];
173
+	camd35_auth = crc32(0L, MD5((unsigned char *)user, strlen(user), dump), 16);
174
+//	fprintf(stderr, "camd35_auth = 0x%08x\n", camd35_auth);
175
+
176
+	MD5((unsigned char *)pass, strlen(pass), dump);
177
+//	fprintf(stderr, "aes_dec_key=0x%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n\n",
178
+//		dump[0], dump[1], dump[2] , dump[3] , dump[4] , dump[5] , dump[6] , dump[7],
179
+//		dump[8], dump[9], dump[10], dump[11], dump[12], dump[13], dump[14], dump[15]
180
+//	);
181
+	AES_set_encrypt_key(dump, 128, &camd35_aes_encrypt_key);
182
+	AES_set_decrypt_key(dump, 128, &camd35_aes_decrypt_key);
183
+}
184
+
185
+static int camd35_recv(uint8_t *data, int *data_len) {
186
+	fprintf(stderr, "%s\n", __func__);
187
+	int i;
188
+
189
+	uint32_t auth_token = (((data[0] << 24) | (data[1] << 16) | (data[2]<<8) | data[3]) & 0xffffffffL);
190
+	fprintf(stderr, "recv auth : 0x%08x\n", auth_token);
191
+
192
+	*data_len -= 4; // Remove header
193
+	memmove(data, data + 4, *data_len); // Remove header
194
+
195
+	char *d = ts_hex_dump(data, *data_len, 16);
196
+	fprintf(stderr, "recv Encrypted data:\n%s\n", d);
197
+	free(d);
198
+
199
+	for (i = 0; i < *data_len; i += 16) // Decrypt payload
200
+		AES_decrypt(data + i, data + i, &camd35_aes_decrypt_key);
201
+
202
+	char *f = ts_hex_dump(data, *data_len, 16);
203
+	fprintf(stderr, "recv Decrypted data:\n%s\n", f);
204
+	free(f);
205
+
206
+	return 0;
207
+}
208
+
209
+static int camd35_send(uint8_t *data, uint8_t data_len) {
210
+	fprintf(stderr, "%s\n", __func__);
211
+	unsigned int i;
212
+	uint8_t buf[BUF_SIZE];
213
+	uint8_t *bdata = buf + 4;
214
+
215
+	init_4b(camd35_auth, buf); // Put authentication token
216
+	memcpy(bdata, data, data_len); // Put data
217
+
218
+	for (i = 0; i < data_len; i += 16) // Encrypt payload
219
+		AES_encrypt(data + i, bdata + i, &camd35_aes_encrypt_key);
220
+
221
+	char *d = ts_hex_dump(bdata, data_len, 16);
222
+	fprintf(stderr, "ECM SEND (encrypted):\n%s\n", d);
223
+	free(d);
224
+
225
+	savefile(buf, data_len + 4, TYPE_ECM);
226
+
227
+	int dlen = data_len + 4;
228
+	camd35_recv(buf, &dlen);
229
+	return 0;
230
+}
231
+
232
+
233
+static int camd35_send_ecm(uint16_t service_id, uint16_t ca_id, uint16_t idx, uint8_t *data, uint8_t data_len) {
234
+	fprintf(stderr, "%s\n", __func__);
235
+	uint8_t buf[BUF_SIZE];
236
+	uint32_t provider_id = 0;
237
+	uint32_t crc = crc32(0L, data, data_len);
238
+
239
+	memset(buf, 0xff, BUF_SIZE);
240
+
241
+	memset(buf, 0, HDR_LEN);
242
+	buf[1] = data_len;
243
+	init_4b(crc        , buf + 4);
244
+	init_2b(service_id , buf + 8);
245
+	init_2b(ca_id      , buf + 10);
246
+	init_4b(provider_id, buf + 12);
247
+	init_2b(idx        , buf + 16);
248
+	buf[18] = 0xff;
249
+	buf[19] = 0xff;
250
+	memcpy(buf + HDR_LEN, data, data_len);
251
+
252
+	int to_send = boundary(4, data_len + HDR_LEN);
253
+
254
+	char *d = ts_hex_dump(buf, to_send, 16);
255
+	fprintf(stderr, "ECM SEND:\n%s\n", d);
256
+	free(d);
257
+
258
+	savefile(buf, to_send, TYPE_ECM);
259
+	return camd35_send(buf, to_send);
260
+}
261
+
262
+static int camd35_send_emm(uint16_t ca_id, uint8_t *data, uint8_t data_len) {
263
+	uint8_t buf[BUF_SIZE];
264
+	uint32_t prov_id = 0;
265
+	uint32_t crc = crc32(0L, data, data_len);
266
+
267
+	memset(buf, 0xff, BUF_SIZE);
268
+
269
+	memset(buf, 0, HDR_LEN);
270
+	buf[0] = 0x06;
271
+	buf[1] = data_len;
272
+	init_4b(crc    , buf + 4);
273
+	init_2b(ca_id  , buf + 10);
274
+	init_4b(prov_id, buf + 12);
275
+	memcpy(buf + HDR_LEN, data, data_len);
276
+
277
+	int to_send = boundary(4, data_len + HDR_LEN);
278
+
279
+	char *d = ts_hex_dump(buf, to_send, 16);
280
+	fprintf(stderr, "EMM SEND:\n%s\n", d);
281
+	free(d);
282
+
283
+	savefile(buf, to_send, TYPE_EMM);
284
+	return camd35_send(buf, to_send);
285
+}
286
+
287
+#define ERR(x) do { fprintf(stderr, "%s", x); return NULL; } while (0)
288
+
289
+static uint8_t *camd35_recv_cw(uint8_t *data, int data_len) {
290
+	char *d;
291
+
292
+	camd35_recv(data, &data_len);
293
+
294
+	if (data_len < 48)
295
+		ERR("len mismatch != 48");
296
+
297
+	if (data[0] < 0x01)
298
+		ERR("Not valid CW response");
299
+
300
+	if (data[1] < 0x10)
301
+		ERR("CW len mismatch != 0x10");
302
+
303
+	d = ts_hex_dump(data, data_len, 16);
304
+	fprintf(stderr, "Recv CW :\n%s\n", d);
305
+	free(d);
306
+
307
+	uint16_t ca_id = (data[10] << 8) | data[11];
308
+	uint16_t idx   = (data[16] << 8) | data[17];
309
+	fprintf(stderr, "CW ca_id: 0x%04x\n", ca_id);
310
+	fprintf(stderr, "CW idx  : 0x%04x\n", idx);
311
+
312
+	d = ts_hex_dump(data + 20, 16, 0);
313
+	fprintf(stderr, "CW      : %s\n", d);
314
+	free(d);
315
+
316
+	return data + 20;
317
+}
318
+
319
+#undef ERR
320
+
321
+void camd35_test() {
322
+	#define test_ecm_len 103
323
+	uint8_t test_ecm[test_ecm_len] = {
324
+		 0x80, 0x70, 0x64, 0x70, 0x62, 0x64, 0x20, 0x76, 0xFF, 0xA8, 0xC1, 0x80, 0x9C, 0xE3, 0xDC, 0xB4, 
325
+		 0xD9, 0xC3, 0xD1, 0xEA, 0x26, 0xFE, 0xF7, 0xE4, 0xA8, 0x26, 0x34, 0x45, 0x51, 0x82, 0x6A, 0xE0, 
326
+		 0x00, 0x37, 0x09, 0x1A, 0xAE, 0xC3, 0x5A, 0xD6, 0xE1, 0xC1, 0x5F, 0x8E, 0x55, 0xC3, 0xA4, 0x88, 
327
+		 0x38, 0x93, 0xDC, 0xD5, 0x9F, 0x10, 0x58, 0xC0, 0xED, 0xB8, 0x4C, 0xED, 0x19, 0x6A, 0x2A, 0xEF, 
328
+		 0x6D, 0xCB, 0x9F, 0x7B, 0x71, 0xC4, 0x29, 0x44, 0x7F, 0xA0, 0x76, 0x80, 0x9E, 0x29, 0x52, 0x4E, 
329
+		 0x19, 0x11, 0xC4, 0xCD, 0xFD, 0x8F, 0x4F, 0xEC, 0x7F, 0x6A, 0xE3, 0x1F, 0x1F, 0x24, 0x0D, 0xEE, 
330
+		 0x7F, 0xF2, 0x35, 0xA4, 0x1C, 0x86, 0x84 };
331
+
332
+	#define test_recv_len 128
333
+	uint8_t test_recv[test_recv_len] = {
334
+		 0x00, 0x67, 0x00, 0x00, 0x99, 0x14, 0x7A, 0xA0, 0x15, 0x22, 0x0B, 0x01, 0x00, 0x00, 0x00, 0x00,
335
+		 0x00, 0x12, 0xFF, 0xFF, 0x80, 0x70, 0x64, 0x70, 0x62, 0x64, 0x20, 0x76, 0xFF, 0xA8, 0xC1, 0x80,
336
+		 0x9C, 0xE3, 0xDC, 0xB4, 0xD9, 0xC3, 0xD1, 0xEA, 0x26, 0xFE, 0xF7, 0xE4, 0xA8, 0x26, 0x34, 0x45,
337
+		 0x51, 0x82, 0x6A, 0xE0, 0x00, 0x37, 0x09, 0x1A, 0xAE, 0xC3, 0x5A, 0xD6, 0xE1, 0xC1, 0x5F, 0x8E,
338
+		 0x55, 0xC3, 0xA4, 0x88, 0x38, 0x93, 0xDC, 0xD5, 0x9F, 0x10, 0x58, 0xC0, 0xED, 0xB8, 0x4C, 0xED,
339
+		 0x19, 0x6A, 0x2A, 0xEF, 0x6D, 0xCB, 0x9F, 0x7B, 0x71, 0xC4, 0x29, 0x44, 0x7F, 0xA0, 0x76, 0x80,
340
+		 0x9E, 0x29, 0x52, 0x4E, 0x19, 0x11, 0xC4, 0xCD, 0xFD, 0x8F, 0x4F, 0xEC, 0x7F, 0x6A, 0xE3, 0x1F,
341
+		 0x1F, 0x24, 0x0D, 0xEE, 0x7F, 0xF2, 0x35, 0xA4, 0x1C, 0x86, 0x84, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
342
+	};
343
+
344
+	#define test_recv_cw_len 52
345
+	uint8_t test_recv_cw[test_recv_cw_len] = {
346
+		 0x11, 0x22, 0x33, 0x44,
347
+		 0x01, 0x10, 0x00, 0x00, 0xB2, 0x05, 0xDF, 0xA0, 0x15, 0x22, 0x0B, 0x01, 0x00, 0x00, 0x00, 0x00,
348
+		 0x00, 0x12, 0xFF, 0xFF, 0xE1, 0x96, 0xE6, 0x5D, 0x09, 0x83, 0x91, 0x1D, 0x85, 0xA4, 0xD1, 0xFA,
349
+		 0xB7, 0x43, 0xAA, 0xA4, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
350
+	};
351
+
352
+	camd35_init_auth(camd35_user, camd35_pass);
353
+
354
+	char *d = ts_hex_dump(test_recv, test_recv_len, 16);
355
+	fprintf(stderr, "%s\n\n", d);
356
+	free(d);
357
+
358
+	camd35_send_ecm(0x1522, 0x0b01, 0x0012, test_ecm, test_ecm_len);
359
+
360
+//	camd35_send_emm(0x0b01, test_ecm, test_ecm_len);
361
+//
362
+//	camd35_recv_cw(test_recv_cw, test_recv_cw_len);
363
+}
364
+
365
+
366
+int main(int argc, char **argv) {
367
+	camd35_test();
368
+	return 0;
369
+
370
+	int fd = 0; // stdin
371
+
372
+	parse_options(argc, argv);
373
+
374
+	struct ts_pat *pat = ts_pat_alloc();
375
+	struct ts_cat *cat = ts_cat_alloc();
376
+	struct ts_pmt *pmt = ts_pmt_alloc();
377
+	struct ts_privsec *emm = ts_privsec_alloc();
378
+	struct ts_privsec *ecm = ts_privsec_alloc();
379
+	struct ts_privsec *last_emm = NULL;
380
+	struct ts_privsec *last_ecm = NULL;
381
+	uint16_t pmt_pid = 0;
382
+
383
+	uint16_t emm_caid = 0, emm_pid = 0;
384
+	uint16_t ecm_caid = 0, ecm_pid = 0;
385
+	uint16_t program_id = 0;
386
+	do {
387
+		uint8_t ts_packet[188];
388
+		ssize_t readen = read(fd, ts_packet, 188);
389
+		if (readen < 188)
390
+			break;
391
+
392
+		uint16_t pid = ts_packet_get_pid(ts_packet);
393
+
394
+		if (pid == 0x00) {
395
+			pat = ts_pat_push_packet(pat, ts_packet);
396
+			if (pat->initialized) {
397
+				int i;
398
+				for (i=0;i<pat->programs_num;i++) {
399
+					struct ts_pat_program *prg = pat->programs[i];
400
+					if (prg->pid) {
401
+						if (prg->program != 0) {
402
+							program_id = prg->program;
403
+							pmt_pid = prg->pid;
404
+						}
405
+					}
406
+				}
407
+				ts_pat_free(&pat);
408
+				pat = ts_pat_alloc();
409
+			}
410
+		}
411
+
412
+		if (pid == 1) {
413
+			cat = ts_cat_push_packet(cat, ts_packet);
414
+			if (cat->initialized) {
415
+				if (req_CA_sys != CA_UNKNOWN)
416
+					ts_get_emm_info(cat, req_CA_sys, &emm_caid, &emm_pid);
417
+				ts_cat_free(&cat);
418
+				cat = ts_cat_alloc();
419
+			}
420
+		}
421
+
422
+		if (pid && pid == pmt_pid) {
423
+			pmt = ts_pmt_push_packet(pmt, ts_packet, pmt_pid);
424
+			if (pmt->initialized) {
425
+				if (req_CA_sys != CA_UNKNOWN)
426
+					ts_get_ecm_info(pmt, req_CA_sys, &ecm_caid, &ecm_pid);
427
+				ts_pmt_free(&pmt);
428
+				pmt = ts_pmt_alloc();
429
+			}
430
+		}
431
+
432
+		if (0 && emm_pid && pid == emm_pid) {
433
+			emm = ts_privsec_push_packet(emm, ts_packet);
434
+			if (emm->initialized) {
435
+				struct ts_header *th = &emm->ts_header;
436
+				struct ts_section_header *sec = emm->section_header;
437
+				//savefile(sec->section_data, sec->section_length + 3, TYPE_EMM);
438
+				char *data = ts_hex_dump(sec->section_data, sec->section_length, 0);
439
+				ts_LOGf("EMM dump | CAID: 0x%04x PID 0x%04x (%5d) Table: 0x%02x (%3d) Length: %4d Data: %s\n",
440
+					emm_caid,
441
+					th->pid, th->pid,
442
+					sec->table_id, sec->table_id,
443
+					sec->section_length + 3,
444
+					data);
445
+				FREE(data);
446
+				ts_privsec_free(&last_emm);
447
+				last_emm = emm;
448
+				emm = ts_privsec_alloc();
449
+			}
450
+		}
451
+
452
+#ifndef min
453
+#define min(a,b) ((a < b) ? a : b)
454
+#endif
455
+
456
+		if (ecm_pid && pid == ecm_pid) {
457
+			ecm = ts_privsec_push_packet(ecm, ts_packet);
458
+			if (ecm->initialized) {
459
+				int is_same = 0;
460
+				struct ts_header *th = &ecm->ts_header;
461
+				struct ts_section_header *sec = ecm->section_header;
462
+				if (last_ecm) {
463
+					is_same = memcmp(
464
+						last_ecm->section_header->section_data,
465
+						ecm->section_header->section_data,
466
+						min(last_ecm->section_header->section_length, ecm->section_header->section_length)) == 0;
467
+				}
468
+				if (!is_same) {
469
+					//savefile(sec->section_data, sec->section_length + 3, TYPE_ECM);
470
+					char *data = ts_hex_dump(sec->section_data, sec->section_length, 0);
471
+					ts_LOGf("ECM dump | CAID: 0x%04x PID 0x%04x (%5d) Table: 0x%02x (%3d) Length: %4d Data: %s\n",
472
+						ecm_caid,
473
+						th->pid, th->pid,
474
+						sec->table_id, sec->table_id,
475
+						sec->section_length + 3,
476
+						data);
477
+					FREE(data);
478
+				} else if (0) {
479
+					ts_LOGf("ECM dump | CAID: 0x%04x PID 0x%04x (%5d) Table: 0x%02x (%3d) Length: %4d Data: --duplicate--\n",
480
+						ecm_caid,
481
+						th->pid, th->pid,
482
+						sec->table_id, sec->table_id,
483
+						sec->section_length + 3);
484
+				}
485
+				ts_privsec_free(&last_ecm);
486
+				last_ecm = ecm;
487
+				ecm = ts_privsec_alloc();
488
+			}
489
+		}
490
+	} while (1);
491
+	ts_pat_free(&pat);
492
+	ts_cat_free(&cat);
493
+	ts_pmt_free(&pmt);
494
+	ts_privsec_free(&emm);
495
+	ts_privsec_free(&ecm);
496
+	ts_privsec_free(&last_emm);
497
+	ts_privsec_free(&last_ecm);
498
+
499
+	if (emm_caid) {
500
+		char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(emm_caid));
501
+		printf("%s PRG_id  : 0x%04x\n", CA_sys, program_id);
502
+		printf("%s CA_id   : 0x%04x\n", CA_sys, emm_caid);
503
+		printf("%s EMM pid : 0x%04x\n", CA_sys, emm_pid);
504
+		printf("%s ECM pid : 0x%04x\n", CA_sys, ecm_pid);
505
+	}
506
+
507
+	exit(0);
508
+}

Loading…
Cancel
Save