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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "libts/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. if (pid != 0x00)
  43. return;
  44. handle_table_changes(pat);
  45. for (i=0;i<ts->pat->programs_num;i++) {
  46. struct ts_pat_program *prg = ts->pat->programs[i];
  47. if (prg->pid) {
  48. if (prg->program != 0) {
  49. ts->pmt_pid = prg->pid;
  50. ts->service_id = prg->program;
  51. }
  52. }
  53. }
  54. }
  55. void process_cat(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  56. if (pid != 0x01)
  57. return;
  58. handle_table_changes(cat);
  59. ts_get_emm_info(ts->cat, ts->req_CA_sys, &ts->emm_caid, &ts->emm_pid);
  60. if (ts->emm_caid) {
  61. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->emm_caid));
  62. ts_LOGf("--- | EMM CAID: 0x%04x (%s)\n", ts->emm_caid, CA_sys);
  63. ts_LOGf("--- | EMM pid : 0x%04x (%s)\n", ts->emm_pid, CA_sys);
  64. } else {
  65. ts_LOGf("*** | ERROR: Can't detect EMM pid.\n");
  66. }
  67. }
  68. void process_pmt(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  69. int i;
  70. if (!pid || pid != ts->pmt_pid)
  71. return;
  72. handle_table_changes(pmt);
  73. pidmap_clear(&ts->pidmap);
  74. pidmap_set(&ts->pidmap, 0x0000); // PAT
  75. pidmap_set(&ts->pidmap, 0x0011); // SDT
  76. pidmap_set(&ts->pidmap, ts->pmt->ts_header.pid); // PMT PID
  77. pidmap_set(&ts->pidmap, ts->pmt->PCR_pid); // PCR
  78. for (i=0;i<ts->pmt->streams_num;i++) {
  79. struct ts_pmt_stream *stream = ts->pmt->streams[i];
  80. pidmap_set(&ts->pidmap, stream->pid); // Data
  81. }
  82. ts_get_ecm_info(ts->pmt, ts->req_CA_sys, &ts->ecm_caid, &ts->ecm_pid);
  83. if (ts->ecm_caid) {
  84. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->ecm_caid));
  85. ts_LOGf("--- | ECM CAID: 0x%04x (%s)\n", ts->ecm_caid, CA_sys);
  86. ts_LOGf("--- | ECM pid : 0x%04x (%s)\n", ts->ecm_pid, CA_sys);
  87. } else {
  88. ts_LOGf("*** | ERROR: Can't detect ECM pid.\n");
  89. }
  90. }
  91. #define dump_sz (16)
  92. #define dump_buf_sz (dump_sz * 6)
  93. static void __process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  94. char dump[dump_buf_sz];
  95. show_ts_pack(ts, pid, "emm", NULL, ts_packet);
  96. if (!ts->emm_send)
  97. return;
  98. ts->emm = ts_privsec_push_packet(ts->emm, ts_packet);
  99. if (!ts->emm->initialized)
  100. return;
  101. struct ts_header *th = &ts->emm->ts_header;
  102. struct ts_section_header *sec = ts->emm->section_header;
  103. if (ts->debug_level >= 2) {
  104. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  105. ts_LOGf("EMM | CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %3d ----------- Data: %s..\n",
  106. ts->emm_caid,
  107. th->pid,
  108. sec->table_id,
  109. sec->section_data_len,
  110. dump);
  111. }
  112. camd_msg_process(ts, camd_msg_alloc_emm(ts->emm_caid, sec->section_data, sec->section_data_len));
  113. ts_privsec_copy(ts->emm, ts->last_emm);
  114. ts_privsec_clear(ts->emm);
  115. }
  116. static void __process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  117. char dump[dump_buf_sz];
  118. ts->ecm = ts_privsec_push_packet(ts->ecm, ts_packet);
  119. if (!ts->ecm->initialized)
  120. return;
  121. if (ts->req_CA_sys == CA_IRDETO) {
  122. int type = ts->ecm->section_header->section_data[4];
  123. if (type != ts->irdeto_ecm) {
  124. ts_privsec_clear(ts->ecm);
  125. return;
  126. }
  127. }
  128. struct ts_header *th = &ts->ecm->ts_header;
  129. struct ts_section_header *sec = ts->ecm->section_header;
  130. int duplicate = ts_privsec_is_same(ts->ecm, ts->last_ecm);
  131. if (!duplicate || ts->is_cw_error) {
  132. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  133. ts_LOGf("ECM | CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %3d IDX: 0x%04x Data: %s..\n",
  134. ts->ecm_caid,
  135. th->pid,
  136. sec->table_id,
  137. sec->section_data_len,
  138. ts->ecm_counter,
  139. dump);
  140. if (ts->is_cw_error)
  141. ts->ecm_counter--;
  142. ts->is_cw_error = 0;
  143. camd_msg_process(ts, camd_msg_alloc_ecm(ts->ecm_caid, ts->service_id, ts->ecm_counter++, sec->section_data, sec->section_data_len));
  144. } else if (ts->debug_level >= 3) {
  145. ts_LOGf("ECM | CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %3d IDX: 0x%04x Data: -dup-\n",
  146. ts->ecm_caid,
  147. th->pid,
  148. sec->table_id,
  149. sec->section_data_len,
  150. ts->ecm_counter - 1);
  151. }
  152. ts_privsec_copy(ts->ecm, ts->last_ecm);
  153. ts_privsec_clear(ts->ecm);
  154. show_ts_pack(ts, pid, !duplicate ? "ecm" : "ec+", NULL, ts_packet);
  155. }
  156. // There are cryptosystems that are puting more than one PSI table
  157. // in TS packet. IRDETO is such example. Because libtsfuncs assumes
  158. // that one ts packet can produce maximum 1 PSI table, the following
  159. // workaround is used for EMM/ECM private sections. Basically we detect
  160. // if after the section there is something else than 0xff (filler) and
  161. // if there is something change ts_packet pointer field to point to
  162. // start of the potential section and reparse section.
  163. void process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  164. int section_end;
  165. if (ts->emm_only)
  166. return;
  167. if (!ts->ecm_pid || ts->ecm_pid != pid)
  168. return;
  169. process_psi:
  170. ts->tmp_ecm = ts_privsec_push_packet(ts->tmp_ecm, ts_packet);
  171. if (!ts->tmp_ecm->initialized) {
  172. __process_ecm(ts, pid, ts_packet);
  173. return;
  174. }
  175. section_end = ts->tmp_ecm->section_header->pointer_field + ts->tmp_ecm->section_header->section_length + 3 + 4 + 1;
  176. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  177. __process_ecm(ts, pid, ts_packet);
  178. ts_packet[4] = ts_packet[4] + ts->tmp_ecm->section_header->section_length + 3;
  179. ts_privsec_clear(ts->tmp_ecm);
  180. goto process_psi;
  181. } else {
  182. __process_ecm(ts, pid, ts_packet);
  183. }
  184. ts_privsec_clear(ts->tmp_ecm);
  185. }
  186. void process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  187. int section_end;
  188. if (!ts->emm_pid || ts->emm_pid != pid)
  189. return;
  190. process_psi:
  191. ts->tmp_emm = ts_privsec_push_packet(ts->tmp_emm, ts_packet);
  192. if (!ts->tmp_emm->initialized) {
  193. __process_emm(ts, pid, ts_packet);
  194. return;
  195. }
  196. section_end = ts->tmp_emm->section_header->pointer_field + ts->tmp_emm->section_header->section_length + 3 + 4 + 1;
  197. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  198. __process_emm(ts, pid, ts_packet);
  199. ts_packet[4] = ts_packet[4] + ts->tmp_emm->section_header->section_length + 3;
  200. ts_privsec_clear(ts->tmp_emm);
  201. goto process_psi;
  202. } else {
  203. __process_emm(ts, pid, ts_packet);
  204. }
  205. ts_privsec_clear(ts->tmp_emm);
  206. }