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 12KB

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