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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <string.h>
  16. #include "data.h"
  17. #include "tables.h"
  18. #include "camd.h"
  19. #include "filter.h"
  20. #include "libtsfuncs/tsfuncs.h"
  21. #include "libfuncs/libfuncs.h"
  22. extern void show_ts_pack(struct ts *ts, uint16_t pid, char *wtf, char *extra, uint8_t *ts_packet);
  23. #define handle_table_changes(TABLE) \
  24. do { \
  25. show_ts_pack(ts, pid, #TABLE, NULL, ts_packet); \
  26. ts->cur##TABLE = ts_##TABLE##_push_packet(ts->cur##TABLE, ts_packet); \
  27. if (!ts->cur##TABLE->initialized) \
  28. return; \
  29. if (ts_##TABLE##_is_same(ts->TABLE, ts->cur##TABLE)) { \
  30. ts_##TABLE##_clear(ts->cur##TABLE); \
  31. return; \
  32. } \
  33. ts_##TABLE##_free(&ts->TABLE); \
  34. ts->TABLE = ts_##TABLE##_copy(ts->cur##TABLE); \
  35. ts_##TABLE##_clear(ts->cur##TABLE); \
  36. if (ts->debug_level >= 1) \
  37. ts_##TABLE##_dump(ts->TABLE); \
  38. } while(0)
  39. void process_pat(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  40. int i;
  41. int num_services = 0;
  42. uint16_t f_service = 0, f_pid = 0;
  43. if (pid != 0x00)
  44. return;
  45. handle_table_changes(pat);
  46. for (i=0;i<ts->pat->programs_num;i++) {
  47. struct ts_pat_program *prg = ts->pat->programs[i];
  48. if (prg->pid && prg->program != 0) {
  49. num_services++;
  50. ts->pmt_pid = prg->pid;
  51. ts->service_id = prg->program;
  52. if (prg->program == ts->forced_service_id) {
  53. f_pid = prg->pid;
  54. f_service = prg->program;
  55. }
  56. }
  57. }
  58. if (f_service && f_pid) {
  59. ts->pmt_pid = f_pid;
  60. ts->service_id = f_service;
  61. }
  62. if (num_services > 1 && !f_service) {
  63. ts_LOGf("PAT | %d services exists. Consider using --input-service parameter.\n",
  64. num_services);
  65. for (i = 0; i < ts->pat->programs_num; i++) {
  66. struct ts_pat_program *prg = ts->pat->programs[i];
  67. if (prg->pid && prg->program != 0) {
  68. ts_LOGf("PAT | Service 0x%04x (%5d) with PMT PID %04x (%d)\n",
  69. prg->program, prg->program,
  70. prg->pid, prg->pid);
  71. }
  72. }
  73. }
  74. ts_LOGf("PAT | Using service 0x%04x (%d), PMT pid: %04x (%d)\n",
  75. ts->service_id, ts->service_id,
  76. ts->pmt_pid, ts->pmt_pid);
  77. if (num_services > 1) {
  78. ts_pat_clear(ts->genpat);
  79. ts->genpat = ts_pat_init(ts->genpat, ts->pat->section_header->ts_id_number);
  80. ts_pat_add_program(ts->genpat, ts->service_id, ts->pmt_pid);
  81. }
  82. }
  83. void process_cat(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  84. if (pid != 0x01)
  85. return;
  86. handle_table_changes(cat);
  87. if (ts->camd.constant_codeword)
  88. return;
  89. if (ts->forced_caid) {
  90. ts->emm_caid = ts->forced_caid;
  91. ts_get_emm_info_by_caid(ts->cat, ts->emm_caid, &ts->emm_pid);
  92. } else {
  93. ts_get_emm_info(ts->cat, ts->req_CA_sys, &ts->emm_caid, &ts->emm_pid);
  94. }
  95. if (ts->forced_emm_pid)
  96. ts_get_emm_info_by_pid(ts->cat, &ts->emm_caid, ts->forced_emm_pid);
  97. if (ts->emm_caid) {
  98. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->emm_caid));
  99. ts_LOGf("--- | EMM CAID: 0x%04x (%s)\n", ts->emm_caid, CA_sys);
  100. if (!ts->forced_emm_pid) {
  101. ts_LOGf("--- | EMM pid : 0x%04x (%s)\n", ts->emm_pid, CA_sys);
  102. } else {
  103. ts_LOGf("--- | EMM pid : 0x%04x (%s) (forced: 0x%04x)\n",
  104. ts->emm_pid, CA_sys, ts->forced_emm_pid);
  105. ts->emm_pid = ts->forced_emm_pid;
  106. }
  107. } else {
  108. ts_LOGf("*** | ERROR: Can't detect EMM pid.\n");
  109. }
  110. }
  111. // Copied from libtsfuncs with added logic to return more than one PID
  112. static int find_CA_descriptor(uint8_t *data, int data_len, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid, uint16_t *CA_pids, unsigned int *n_pids) {
  113. while (data_len >= 2) {
  114. uint8_t tag = data[0];
  115. uint8_t this_length = data[1];
  116. data += 2;
  117. data_len -= 2;
  118. if (tag == 9 && this_length >= 4) {
  119. uint16_t CA_ID = (data[0] << 8) | data[1];
  120. uint16_t CA_PID = ((data[2] & 0x1F) << 8) | data[3];
  121. if (ts_get_CA_sys(CA_ID) == req_CA_type) {
  122. *CA_id = CA_ID;
  123. *CA_pid = CA_PID;
  124. if (*n_pids < MAX_ECM_PIDS) {
  125. unsigned int i, exist = 0;
  126. for (i = 0; i < MAX_ECM_PIDS; i++) {
  127. if (CA_pids[i] == CA_PID) {
  128. exist = 1;
  129. break;
  130. }
  131. }
  132. if (!exist) {
  133. CA_pids[(*n_pids)++] = CA_PID;
  134. }
  135. }
  136. }
  137. }
  138. data_len -= this_length;
  139. data += this_length;
  140. }
  141. return 0;
  142. }
  143. // Copied from libtsfuncs with added logic to return more than one PID
  144. int __ts_get_ecm_info(struct ts_pmt *pmt, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid, uint16_t *CA_pids, unsigned int *n_pids) {
  145. int i, result = find_CA_descriptor(pmt->program_info, pmt->program_info_size, req_CA_type, CA_id, CA_pid, CA_pids, n_pids);
  146. if (!result) {
  147. for(i=0;i<pmt->streams_num;i++) {
  148. struct ts_pmt_stream *stream = pmt->streams[i];
  149. if (stream->ES_info) {
  150. result = find_CA_descriptor(stream->ES_info, stream->ES_info_size, req_CA_type, CA_id, CA_pid, CA_pids, n_pids);
  151. if (result)
  152. break;
  153. }
  154. }
  155. }
  156. return result;
  157. }
  158. void process_pmt(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  159. int i;
  160. if (!pid || pid != ts->pmt_pid)
  161. return;
  162. // Mark PMT as valid and the received timestamp
  163. ts->last_pmt_ts = time(NULL);
  164. ts->have_valid_pmt = 1;
  165. handle_table_changes(pmt);
  166. pidmap_clear(&ts->pidmap);
  167. pidmap_set(&ts->pidmap, 0x0000); // PAT
  168. pidmap_set(&ts->pidmap, 0x0011); // SDT
  169. if (ts->nit_passthrough)
  170. pidmap_set(&ts->pidmap, 0x0010); // NIT
  171. if (ts->eit_passthrough)
  172. pidmap_set(&ts->pidmap, 0x0012); // EIT
  173. if (ts->tdt_passthrough)
  174. pidmap_set(&ts->pidmap, 0x0014); // TDT/TOT
  175. pidmap_set(&ts->pidmap, ts->pmt->ts_header.pid); // PMT PID
  176. pidmap_set(&ts->pidmap, ts->pmt->PCR_pid); // PCR
  177. for (i=0;i<ts->pmt->streams_num;i++) {
  178. struct ts_pmt_stream *stream = ts->pmt->streams[i];
  179. pidmap_set(&ts->pidmap, stream->pid); // Data
  180. }
  181. if (ts->camd.constant_codeword)
  182. return;
  183. ts->n_ecm_pids = 0;
  184. if (ts->forced_caid) {
  185. ts->ecm_caid = ts->forced_caid;
  186. ts_get_ecm_info_by_caid(ts->pmt, ts->ecm_caid, &ts->ecm_pid);
  187. ts->n_ecm_pids = 1; // TODO/FIXME: We should get the list of PIDS similar to how __ts_get_ecm_info does it
  188. ts->ecm_pids[0] = ts->ecm_pid;
  189. } else {
  190. __ts_get_ecm_info(ts->pmt, ts->req_CA_sys, &ts->ecm_caid, &ts->ecm_pid, &ts->ecm_pids[0], &ts->n_ecm_pids);
  191. }
  192. if (ts->forced_ecm_pid)
  193. ts_get_ecm_info_by_pid(ts->pmt, &ts->ecm_caid, ts->forced_ecm_pid);
  194. if (ts->ecm_caid) {
  195. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->ecm_caid));
  196. ts_LOGf("--- | ECM CAID: 0x%04x (%s)\n", ts->ecm_caid, CA_sys);
  197. if (!ts->forced_ecm_pid) {
  198. ts_LOGf("--- | ECM pid : 0x%04x (%s)\n", ts->ecm_pid, CA_sys);
  199. } else {
  200. ts_LOGf("--- | ECM pid : 0x%04x (%s) (forced: 0x%04x)\n",
  201. ts->ecm_pid, CA_sys, ts->forced_ecm_pid);
  202. ts->ecm_pid = ts->forced_ecm_pid;
  203. }
  204. if (ts->n_ecm_pids > 1) {
  205. for (i = 0; i < (int)ts->n_ecm_pids; i++) {
  206. ts_LOGf("--- | ECM pid : 0x%04x (%s) idx:%d\n", ts->ecm_pids[i], CA_sys, i);
  207. }
  208. }
  209. } else {
  210. ts_LOGf("*** | ERROR: Can't detect ECM pid.\n");
  211. }
  212. if (ts->req_CA_sys == CA_IRDETO) {
  213. memset(ts->irdeto_chid, 0, sizeof(ts->irdeto_chid));
  214. ts->irdeto_max_chids = 0;
  215. }
  216. }
  217. static int sdt_parse_service_name_desc(
  218. int desc_len, uint8_t *desc,
  219. uint8_t *service_type,
  220. uint8_t *pname_len, uint8_t **pname,
  221. uint8_t *sname_len, uint8_t **sname)
  222. {
  223. int ofs = 0;
  224. *pname_len = 0;
  225. *sname_len = 0;
  226. *pname = NULL;
  227. *sname = NULL;
  228. while (ofs + 2 < desc_len) {
  229. uint8_t tag = desc[ofs++];
  230. uint8_t len = desc[ofs++];
  231. if (tag != 0x48) {
  232. ofs += len;
  233. continue;
  234. }
  235. // Parse descriptor 0x48 - service_descriptor
  236. // +3 == +1 for service type, +1 for provider len, +1 for service len
  237. if (ofs + 3 > desc_len)
  238. break;
  239. *service_type = desc[ofs++];
  240. *pname_len = desc[ofs++];
  241. if (*pname_len)
  242. *pname = desc + ofs;
  243. ofs += *pname_len;
  244. if (ofs > desc_len)
  245. break;
  246. *sname_len = desc[ofs++];
  247. if (*sname_len)
  248. *sname = desc + ofs;
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. void process_sdt(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  254. int i;
  255. if (pid != 0x11)
  256. return;
  257. handle_table_changes(sdt);
  258. for(i=0;i<ts->sdt->streams_num;i++) {
  259. struct ts_sdt_stream *stream = ts->sdt->streams[i];
  260. uint8_t service_type;
  261. uint8_t *pname, *sname;
  262. uint8_t pname_len, sname_len;
  263. if (sdt_parse_service_name_desc(
  264. stream->descriptor_size, stream->descriptor_data,
  265. &service_type,
  266. &pname_len, &pname, &sname_len, &sname))
  267. {
  268. int r;
  269. for (r = 0; r < pname_len; r++) {
  270. if (pname[r] < ' ')
  271. pname[r] = '*';
  272. }
  273. for (r = 0; r < sname_len; r++) {
  274. if (sname[r] < ' ')
  275. sname[r] = '*';
  276. }
  277. ts_LOGf("SDT | Service 0x%04x (%5d) Type: 0x%02x (%s) Provider: \"%.*s\" Service: \"%.*s\"\n",
  278. stream->service_id, stream->service_id,
  279. service_type,
  280. // The service types are described in Table 87 of
  281. // ETSI EN 300 468 v1.12.1 and also in annex I of the
  282. // same document.
  283. service_type == 0x01 ? "Tv" :
  284. service_type == 0x02 ? "Radio" :
  285. service_type == 0x11 ? "Tv/HD" :
  286. service_type == 0x16 ? "Tv/h264" :
  287. service_type == 0x19 ? "Tv/HD/h264" :
  288. service_type == 0x1c ? "Tv/3d" : "unknown",
  289. pname_len, (char *)pname,
  290. sname_len, (char *)sname);
  291. } else {
  292. ts_LOGf("SDT | Service 0x%04x (%5d)\n",
  293. stream->service_id, stream->service_id);
  294. }
  295. }
  296. }
  297. #define dump_sz (15)
  298. #define dump_buf_sz (dump_sz * 6)
  299. static void __process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  300. char dump[dump_buf_sz];
  301. show_ts_pack(ts, pid, "emm", NULL, ts_packet);
  302. ts->emm_input_count++;
  303. ts->emm = ts_privsec_push_packet(ts->emm, ts_packet);
  304. if (!ts->emm->initialized)
  305. return;
  306. struct ts_header *th = &ts->emm->ts_header;
  307. struct ts_section_header *sec = ts->emm->section_header;
  308. int emm_ok = 1;
  309. if (ts->emm_filters_num)
  310. emm_ok = filter_match_emm(ts, sec->section_data, sec->section_data_len);
  311. if (ts->debug_level >= 2) {
  312. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  313. ts_LOGf("EMM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d %s %s..\n",
  314. ts->service_id,
  315. ts->emm_caid,
  316. th->pid,
  317. sec->table_id,
  318. sec->section_data_len,
  319. emm_ok == 1 ? "Data:" : "SKIP:",
  320. dump);
  321. }
  322. if (emm_ok && sec->section_data_len < CAMD35_DATA_SIZE)
  323. camd_process_packet(ts, camd_msg_alloc(EMM_MSG, ts->emm_caid, ts->service_id, sec->section_data, sec->section_data_len));
  324. else
  325. ts->emm_skipped_count++;
  326. ts_privsec_copy(ts->emm, ts->last_emm);
  327. ts_privsec_clear(ts->emm);
  328. }
  329. static void __process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  330. char dump[dump_buf_sz];
  331. ts->ecm = ts_privsec_push_packet(ts->ecm, ts_packet);
  332. if (!ts->ecm->initialized)
  333. return;
  334. if (ts->req_CA_sys == CA_IRDETO) {
  335. uint8_t idx = ts->ecm->section_header->section_data[4];
  336. uint8_t max_idx = ts->ecm->section_header->section_data[5];
  337. uint16_t chid = (ts->ecm->section_header->section_data[6] << 8) | ts->ecm->section_header->section_data[7];
  338. if (max_idx != ts->irdeto_max_chids) {
  339. memset(ts->irdeto_chid, 0, sizeof(ts->irdeto_chid));
  340. ts->irdeto_max_chids = max_idx;
  341. }
  342. bool ecm_ok = false;
  343. const char *filter_type;
  344. switch (ts->irdeto_ecm_filter_type) {
  345. case IRDETO_FILTER_IDX : ecm_ok = (idx == ts->irdeto_ecm_idx); filter_type = " BY IDX"; break;
  346. case IRDETO_FILTER_CHID: ecm_ok = (chid == ts->irdeto_ecm_chid); filter_type = " BY CHID"; break;
  347. }
  348. if (ts->irdeto_chid[idx].seen < 1) {
  349. ts_LOGf("CAS | Seen Irdeto CHID 0x%04x (idx %u/%u)%s%s\n", chid, idx, max_idx,
  350. ecm_ok ? " *SELECTED*" : "",
  351. ecm_ok ? filter_type : "");
  352. ts->irdeto_chid[idx].seen++;
  353. }
  354. if (!ecm_ok) {
  355. ts_privsec_clear(ts->ecm);
  356. return;
  357. }
  358. }
  359. struct ts_header *th = &ts->ecm->ts_header;
  360. struct ts_section_header *sec = ts->ecm->section_header;
  361. // ECMs should be in these tables.
  362. if (sec->section_data[0] != 0x80 && sec->section_data[0] != 0x81) {
  363. ts_privsec_clear(ts->ecm);
  364. return;
  365. }
  366. int duplicate = ts_privsec_is_same(ts->ecm, ts->last_ecm);
  367. if (duplicate && !ts->is_cw_error)
  368. ts->ecm_duplicate_count++;
  369. if (!ts->ecm_change_time.tv_sec && !ts->ecm_change_time.tv_usec) // The first time
  370. gettimeofday(&ts->ecm_change_time, NULL);
  371. if (!duplicate || ts->is_cw_error) {
  372. if (ts->ecm_cw_log) {
  373. struct timeval tv;
  374. gettimeofday(&tv, NULL);
  375. ts_LOGf("ECC | SID 0x%04x ------------ EcmChng: %5llu ms\n",
  376. ts->service_id,
  377. timeval_diff_msec(&ts->ecm_change_time, &tv));
  378. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  379. ts_LOGf("ECM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d Data: %s..\n",
  380. ts->service_id,
  381. ts->ecm_caid,
  382. th->pid,
  383. sec->table_id,
  384. sec->section_data_len,
  385. dump);
  386. }
  387. gettimeofday(&ts->ecm_change_time, NULL);
  388. ts->is_cw_error = 0;
  389. camd_process_packet(ts, camd_msg_alloc(ECM_MSG, ts->ecm_caid, ts->service_id, sec->section_data, sec->section_data_len));
  390. } else if (ts->debug_level >= 3) {
  391. ts_LOGf("ECM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d Data: -dup-\n",
  392. ts->service_id,
  393. ts->ecm_caid,
  394. th->pid,
  395. sec->table_id,
  396. sec->section_data_len);
  397. }
  398. ts_privsec_copy(ts->ecm, ts->last_ecm);
  399. ts_privsec_clear(ts->ecm);
  400. show_ts_pack(ts, pid, !duplicate ? "ecm" : "ec+", NULL, ts_packet);
  401. }
  402. // There are cryptosystems that are puting more than one PSI table
  403. // in TS packet. IRDETO is such example. Because libtsfuncs assumes
  404. // that one ts packet can produce maximum 1 PSI table, the following
  405. // workaround is used for EMM/ECM private sections. Basically we detect
  406. // if after the section there is something else than 0xff (filler) and
  407. // if there is something change ts_packet pointer field to point to
  408. // start of the potential section and reparse section.
  409. void process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  410. int section_end;
  411. if (!ts->process_ecm)
  412. return;
  413. if (!ts->ecm_pid || ts->ecm_pid != pid)
  414. return;
  415. process_psi:
  416. ts->tmp_ecm = ts_privsec_push_packet(ts->tmp_ecm, ts_packet);
  417. if (!ts->tmp_ecm->initialized) {
  418. __process_ecm(ts, pid, ts_packet);
  419. return;
  420. }
  421. section_end = ts->tmp_ecm->section_header->pointer_field + ts->tmp_ecm->section_header->section_length + 3 + 4 + 1;
  422. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  423. __process_ecm(ts, pid, ts_packet);
  424. ts_packet[4] = ts_packet[4] + ts->tmp_ecm->section_header->section_length + 3;
  425. ts_privsec_clear(ts->tmp_ecm);
  426. goto process_psi;
  427. } else {
  428. __process_ecm(ts, pid, ts_packet);
  429. }
  430. ts_privsec_clear(ts->tmp_ecm);
  431. }
  432. void process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  433. int section_end;
  434. if (!ts->process_emm)
  435. return;
  436. process_psi:
  437. ts->tmp_emm = ts_privsec_push_packet(ts->tmp_emm, ts_packet);
  438. if (!ts->tmp_emm->initialized) {
  439. __process_emm(ts, pid, ts_packet);
  440. return;
  441. }
  442. section_end = ts->tmp_emm->section_header->pointer_field + ts->tmp_emm->section_header->section_length + 3 + 4 + 1;
  443. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  444. __process_emm(ts, pid, ts_packet);
  445. ts_packet[4] = ts_packet[4] + ts->tmp_emm->section_header->section_length + 3;
  446. ts_privsec_clear(ts->tmp_emm);
  447. goto process_psi;
  448. } else {
  449. __process_emm(ts, pid, ts_packet);
  450. }
  451. ts_privsec_clear(ts->tmp_emm);
  452. }