tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tables.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Process PSI tables
  3. * Copyright (C) 2011 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License (COPYING file) for more details.
  13. *
  14. */
  15. #include "data.h"
  16. #include "tables.h"
  17. #include "camd.h"
  18. #include "libtsfuncs/tsfuncs.h"
  19. #include "libfuncs/libfuncs.h"
  20. extern void show_ts_pack(struct ts *ts, uint16_t pid, char *wtf, char *extra, uint8_t *ts_packet);
  21. #define handle_table_changes(TABLE) \
  22. do { \
  23. show_ts_pack(ts, pid, #TABLE, NULL, ts_packet); \
  24. ts->cur##TABLE = ts_##TABLE##_push_packet(ts->cur##TABLE, ts_packet); \
  25. if (!ts->cur##TABLE->initialized) \
  26. return; \
  27. if (ts_##TABLE##_is_same(ts->TABLE, ts->cur##TABLE)) { \
  28. ts_##TABLE##_clear(ts->cur##TABLE); \
  29. return; \
  30. } \
  31. ts_##TABLE##_free(&ts->TABLE); \
  32. ts->TABLE = ts_##TABLE##_copy(ts->cur##TABLE); \
  33. ts_##TABLE##_clear(ts->cur##TABLE); \
  34. if (ts->debug_level >= 1) \
  35. ts_##TABLE##_dump(ts->TABLE); \
  36. } while(0)
  37. void process_pat(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  38. int i;
  39. int num_services = 0;
  40. uint16_t f_service = 0, f_pid = 0;
  41. if (pid != 0x00)
  42. return;
  43. handle_table_changes(pat);
  44. for (i=0;i<ts->pat->programs_num;i++) {
  45. struct ts_pat_program *prg = ts->pat->programs[i];
  46. if (prg->pid && prg->program != 0) {
  47. num_services++;
  48. ts->pmt_pid = prg->pid;
  49. ts->service_id = prg->program;
  50. if (prg->program == ts->forced_service_id) {
  51. f_pid = prg->pid;
  52. f_service = prg->program;
  53. }
  54. }
  55. }
  56. if (f_service && f_pid) {
  57. ts->pmt_pid = f_pid;
  58. ts->service_id = f_service;
  59. }
  60. if (num_services > 1 && !f_service) {
  61. ts_LOGf("PAT | %d services exists. Consider using --input-service parameter.\n",
  62. num_services);
  63. for (i = 0; i < ts->pat->programs_num; i++) {
  64. struct ts_pat_program *prg = ts->pat->programs[i];
  65. if (prg->pid && prg->program != 0) {
  66. ts_LOGf("PAT | Service 0x%04x (%5d) with PMT PID %04x (%d)\n",
  67. prg->program, prg->program,
  68. prg->pid, prg->pid);
  69. }
  70. }
  71. }
  72. ts_LOGf("PAT | Using service 0x%04x (%d), PMT pid: %04x (%d)\n",
  73. ts->service_id, ts->service_id,
  74. ts->pmt_pid, ts->pmt_pid);
  75. if (num_services > 1) {
  76. ts_pat_clear(ts->genpat);
  77. ts->genpat = ts_pat_init(ts->genpat, ts->pat->section_header->ts_id_number);
  78. ts_pat_add_program(ts->genpat, ts->service_id, ts->pmt_pid);
  79. }
  80. }
  81. void process_cat(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  82. if (pid != 0x01)
  83. return;
  84. handle_table_changes(cat);
  85. if (ts->camd.constant_codeword)
  86. return;
  87. if (ts->forced_caid) {
  88. ts->emm_caid = ts->forced_caid;
  89. ts_get_emm_info_by_caid(ts->cat, ts->emm_caid, &ts->emm_pid);
  90. } else {
  91. ts_get_emm_info(ts->cat, ts->req_CA_sys, &ts->emm_caid, &ts->emm_pid);
  92. }
  93. if (ts->forced_emm_pid)
  94. ts_get_emm_info_by_pid(ts->cat, &ts->emm_caid, ts->forced_emm_pid);
  95. if (ts->emm_caid) {
  96. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->emm_caid));
  97. ts_LOGf("--- | EMM CAID: 0x%04x (%s)\n", ts->emm_caid, CA_sys);
  98. if (!ts->forced_emm_pid) {
  99. ts_LOGf("--- | EMM pid : 0x%04x (%s)\n", ts->emm_pid, CA_sys);
  100. } else {
  101. ts_LOGf("--- | EMM pid : 0x%04x (%s) (forced: 0x%04x)\n",
  102. ts->emm_pid, CA_sys, ts->forced_emm_pid);
  103. ts->emm_pid = ts->forced_emm_pid;
  104. }
  105. } else {
  106. ts_LOGf("*** | ERROR: Can't detect EMM pid.\n");
  107. }
  108. }
  109. void process_pmt(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  110. int i;
  111. if (!pid || pid != ts->pmt_pid)
  112. return;
  113. handle_table_changes(pmt);
  114. pidmap_clear(&ts->pidmap);
  115. pidmap_set(&ts->pidmap, 0x0000); // PAT
  116. pidmap_set(&ts->pidmap, 0x0011); // SDT
  117. if (ts->nit_passthrough)
  118. pidmap_set(&ts->pidmap, 0x0010); // NIT
  119. if (ts->eit_passthrough)
  120. pidmap_set(&ts->pidmap, 0x0012); // EIT
  121. if (ts->tdt_passthrough)
  122. pidmap_set(&ts->pidmap, 0x0014); // TDT/TOT
  123. pidmap_set(&ts->pidmap, ts->pmt->ts_header.pid); // PMT PID
  124. pidmap_set(&ts->pidmap, ts->pmt->PCR_pid); // PCR
  125. for (i=0;i<ts->pmt->streams_num;i++) {
  126. struct ts_pmt_stream *stream = ts->pmt->streams[i];
  127. pidmap_set(&ts->pidmap, stream->pid); // Data
  128. }
  129. if (ts->camd.constant_codeword)
  130. return;
  131. if (ts->forced_caid) {
  132. ts->ecm_caid = ts->forced_caid;
  133. ts_get_ecm_info_by_caid(ts->pmt, ts->ecm_caid, &ts->ecm_pid);
  134. } else {
  135. ts_get_ecm_info(ts->pmt, ts->req_CA_sys, &ts->ecm_caid, &ts->ecm_pid);
  136. }
  137. if (ts->forced_ecm_pid)
  138. ts_get_ecm_info_by_pid(ts->pmt, &ts->ecm_caid, ts->forced_ecm_pid);
  139. if (ts->ecm_caid) {
  140. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->ecm_caid));
  141. ts_LOGf("--- | ECM CAID: 0x%04x (%s)\n", ts->ecm_caid, CA_sys);
  142. if (!ts->forced_ecm_pid) {
  143. ts_LOGf("--- | ECM pid : 0x%04x (%s)\n", ts->ecm_pid, CA_sys);
  144. } else {
  145. ts_LOGf("--- | ECM pid : 0x%04x (%s) (forced: 0x%04x)\n",
  146. ts->ecm_pid, CA_sys, ts->forced_ecm_pid);
  147. ts->ecm_pid = ts->forced_ecm_pid;
  148. }
  149. } else {
  150. ts_LOGf("*** | ERROR: Can't detect ECM pid.\n");
  151. }
  152. }
  153. static int sdt_parse_service_name_desc(
  154. int desc_len, uint8_t *desc,
  155. uint8_t *pname_len, uint8_t **pname,
  156. uint8_t *sname_len, uint8_t **sname)
  157. {
  158. int ofs = 0;
  159. *pname_len = 0;
  160. *sname_len = 0;
  161. *pname = NULL;
  162. *sname = NULL;
  163. while (ofs + 2 < desc_len) {
  164. uint8_t tag = desc[ofs++];
  165. uint8_t len = desc[ofs++];
  166. if (tag != 0x48) {
  167. ofs += len;
  168. continue;
  169. }
  170. // Parse descriptor 0x48 - Service name descriptor
  171. // +3 == +1 for service type, +1 for provider len, +1 for service len
  172. if (ofs + 3 > desc_len)
  173. break;
  174. ofs++; // Skip service type
  175. *pname_len = desc[ofs++];
  176. if (*pname_len)
  177. *pname = desc + ofs;
  178. ofs += *pname_len;
  179. if (ofs > desc_len)
  180. break;
  181. *sname_len = desc[ofs++];
  182. if (*sname_len)
  183. *sname = desc + ofs;
  184. ofs += *sname_len;
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. void process_sdt(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  190. int i;
  191. if (pid != 0x11)
  192. return;
  193. handle_table_changes(sdt);
  194. for(i=0;i<ts->sdt->streams_num;i++) {
  195. struct ts_sdt_stream *stream = ts->sdt->streams[i];
  196. uint8_t *pname, *sname;
  197. uint8_t pname_len, sname_len;
  198. if (sdt_parse_service_name_desc(
  199. stream->descriptor_size, stream->descriptor_data,
  200. &pname_len, &pname, &sname_len, &sname))
  201. {
  202. int r;
  203. for (r = 0; r < pname_len; r++) {
  204. if (pname[r] < ' ')
  205. pname[r] = '*';
  206. }
  207. for (r = 0; r < sname_len; r++) {
  208. if (sname[r] < ' ')
  209. sname[r] = '*';
  210. }
  211. ts_LOGf("SDT | Service 0x%04x (%5d) Provider: \"%.*s\" Service: \"%.*s\"\n",
  212. stream->service_id, stream->service_id,
  213. pname_len, (char *)pname,
  214. sname_len, (char *)sname);
  215. } else {
  216. ts_LOGf("SDT | Service 0x%04x (%5d)\n",
  217. stream->service_id, stream->service_id);
  218. }
  219. }
  220. }
  221. #define dump_sz (15)
  222. #define dump_buf_sz (dump_sz * 6)
  223. static void __process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  224. char dump[dump_buf_sz];
  225. show_ts_pack(ts, pid, "emm", NULL, ts_packet);
  226. if (!ts->emm_send)
  227. return;
  228. ts->emm = ts_privsec_push_packet(ts->emm, ts_packet);
  229. if (!ts->emm->initialized)
  230. return;
  231. struct ts_header *th = &ts->emm->ts_header;
  232. struct ts_section_header *sec = ts->emm->section_header;
  233. if (ts->debug_level >= 2) {
  234. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  235. ts_LOGf("EMM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d Data: %s..\n",
  236. ts->service_id,
  237. ts->emm_caid,
  238. th->pid,
  239. sec->table_id,
  240. sec->section_data_len,
  241. dump);
  242. }
  243. camd_process_packet(ts, camd_msg_alloc(EMM_MSG, ts->emm_caid, ts->service_id, sec->section_data, sec->section_data_len));
  244. ts_privsec_copy(ts->emm, ts->last_emm);
  245. ts_privsec_clear(ts->emm);
  246. }
  247. static void __process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  248. char dump[dump_buf_sz];
  249. ts->ecm = ts_privsec_push_packet(ts->ecm, ts_packet);
  250. if (!ts->ecm->initialized)
  251. return;
  252. if (ts->req_CA_sys == CA_IRDETO) {
  253. int type = ts->ecm->section_header->section_data[4];
  254. if (type != ts->irdeto_ecm) {
  255. ts_privsec_clear(ts->ecm);
  256. return;
  257. }
  258. }
  259. struct ts_header *th = &ts->ecm->ts_header;
  260. struct ts_section_header *sec = ts->ecm->section_header;
  261. // ECMs should be in these tables.
  262. if (sec->section_data[0] != 0x80 && sec->section_data[0] != 0x81) {
  263. ts_privsec_clear(ts->ecm);
  264. return;
  265. }
  266. int duplicate = ts_privsec_is_same(ts->ecm, ts->last_ecm);
  267. if (duplicate && !ts->is_cw_error)
  268. ts->ecm_duplicate_count++;
  269. if (!ts->ecm_change_time.tv_sec)
  270. gettimeofday(&ts->ecm_change_time, NULL);
  271. if (!duplicate || ts->is_cw_error) {
  272. if (ts->ecm_cw_log) {
  273. struct timeval tv;
  274. gettimeofday(&tv, NULL);
  275. ts_LOGf("ECC | SID 0x%04x ------------ EcmChng: %5llu ms\n",
  276. ts->service_id,
  277. timeval_diff_msec(&ts->ecm_change_time, &tv));
  278. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  279. ts_LOGf("ECM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d Data: %s..\n",
  280. ts->service_id,
  281. ts->ecm_caid,
  282. th->pid,
  283. sec->table_id,
  284. sec->section_data_len,
  285. dump);
  286. gettimeofday(&ts->ecm_change_time, NULL);
  287. }
  288. ts->is_cw_error = 0;
  289. camd_process_packet(ts, camd_msg_alloc(ECM_MSG, ts->ecm_caid, ts->service_id, sec->section_data, sec->section_data_len));
  290. } else if (ts->debug_level >= 3) {
  291. ts_LOGf("ECM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d Data: -dup-\n",
  292. ts->service_id,
  293. ts->ecm_caid,
  294. th->pid,
  295. sec->table_id,
  296. sec->section_data_len);
  297. }
  298. ts_privsec_copy(ts->ecm, ts->last_ecm);
  299. ts_privsec_clear(ts->ecm);
  300. show_ts_pack(ts, pid, !duplicate ? "ecm" : "ec+", NULL, ts_packet);
  301. }
  302. // There are cryptosystems that are puting more than one PSI table
  303. // in TS packet. IRDETO is such example. Because libtsfuncs assumes
  304. // that one ts packet can produce maximum 1 PSI table, the following
  305. // workaround is used for EMM/ECM private sections. Basically we detect
  306. // if after the section there is something else than 0xff (filler) and
  307. // if there is something change ts_packet pointer field to point to
  308. // start of the potential section and reparse section.
  309. void process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  310. int section_end;
  311. if (ts->emm_only)
  312. return;
  313. if (!ts->ecm_pid || ts->ecm_pid != pid)
  314. return;
  315. process_psi:
  316. ts->tmp_ecm = ts_privsec_push_packet(ts->tmp_ecm, ts_packet);
  317. if (!ts->tmp_ecm->initialized) {
  318. __process_ecm(ts, pid, ts_packet);
  319. return;
  320. }
  321. section_end = ts->tmp_ecm->section_header->pointer_field + ts->tmp_ecm->section_header->section_length + 3 + 4 + 1;
  322. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  323. __process_ecm(ts, pid, ts_packet);
  324. ts_packet[4] = ts_packet[4] + ts->tmp_ecm->section_header->section_length + 3;
  325. ts_privsec_clear(ts->tmp_ecm);
  326. goto process_psi;
  327. } else {
  328. __process_ecm(ts, pid, ts_packet);
  329. }
  330. ts_privsec_clear(ts->tmp_ecm);
  331. }
  332. void process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  333. int section_end;
  334. if (!ts->emm_pid || ts->emm_pid != pid)
  335. return;
  336. process_psi:
  337. ts->tmp_emm = ts_privsec_push_packet(ts->tmp_emm, ts_packet);
  338. if (!ts->tmp_emm->initialized) {
  339. __process_emm(ts, pid, ts_packet);
  340. return;
  341. }
  342. section_end = ts->tmp_emm->section_header->pointer_field + ts->tmp_emm->section_header->section_length + 3 + 4 + 1;
  343. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  344. __process_emm(ts, pid, ts_packet);
  345. ts_packet[4] = ts_packet[4] + ts->tmp_emm->section_header->section_length + 3;
  346. ts_privsec_clear(ts->tmp_emm);
  347. goto process_psi;
  348. } else {
  349. __process_emm(ts, pid, ts_packet);
  350. }
  351. ts_privsec_clear(ts->tmp_emm);
  352. }