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.

data.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Data definitions
  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. #ifndef DATA_H
  19. #define DATA_H
  20. #include <pthread.h>
  21. #include <limits.h>
  22. #include <openssl/aes.h>
  23. #include <openssl/des.h>
  24. #include <openssl/md5.h>
  25. #include "libfuncs/libfuncs.h"
  26. #include "libtsfuncs/tsfuncs.h"
  27. // 7 * 188
  28. #define FRAME_SIZE 1316
  29. // How much seconds to assume the key is valid
  30. #define KEY_VALID_TIME 10
  31. #define EMM_QUEUE_HARD_LIMIT 10000
  32. #define EMM_QUEUE_SOFT_LIMIT 1000
  33. #define ECM_QUEUE_HARD_LIMIT 10
  34. #define ECM_QUEUE_SOFT_LIMIT 3
  35. // 64k should be enough for everybody
  36. #define THREAD_STACK_SIZE (64 * 1024)
  37. struct notify {
  38. pthread_t thread; /* Thread handle */
  39. QUEUE *notifications; /* Notification queue */
  40. char ident[512]; /* tsdecrypt ident (set by -i) */
  41. char program[512]; /* What program to exec */
  42. };
  43. #define CODEWORD_LENGTH 16
  44. typedef void csakey_t;
  45. struct key {
  46. uint8_t cw[CODEWORD_LENGTH];
  47. csakey_t *csakey;
  48. int is_valid_cw;
  49. time_t ts; // At what time the key is set
  50. struct timeval ts_keyset; // At what time the key is set
  51. };
  52. // 4 auth header, 20 header size, 256 max data size, 16 potential padding
  53. #define CAMD35_HDR_LEN (20)
  54. #define CAMD35_BUF_LEN (4 + CAMD35_HDR_LEN + 256 + 16)
  55. // When this limit is reached invalid_cw flag is set.
  56. #define ECM_RECV_ERRORS_LIMIT 10
  57. // When this limit is reached camd_reconnect is called.
  58. #define EMM_RECV_ERRORS_LIMIT 100
  59. struct camd;
  60. struct ts;
  61. enum msg_type { EMM_MSG, ECM_MSG };
  62. struct camd_msg {
  63. enum msg_type type;
  64. uint16_t ca_id;
  65. uint16_t service_id;
  66. uint8_t data_len;
  67. uint8_t data[255];
  68. struct ts *ts;
  69. };
  70. enum camd_proto {
  71. CAMD_CS378X,
  72. CAMD_NEWCAMD,
  73. };
  74. struct camd_ops {
  75. char *ident;
  76. enum camd_proto proto;
  77. int (*connect)(struct camd *c);
  78. void (*disconnect)(struct camd *c);
  79. int (*reconnect)(struct camd *c);
  80. int (*do_emm)(struct camd *c, struct camd_msg *msg);
  81. int (*do_ecm)(struct camd *c, struct camd_msg *msg);
  82. int (*get_cw)(struct camd *c, uint16_t *ca_id, uint16_t *idx, uint8_t *cw);
  83. };
  84. struct cs378x {
  85. // cs378x private data
  86. uint8_t buf[CAMD35_BUF_LEN];
  87. AES_KEY aes_encrypt_key;
  88. AES_KEY aes_decrypt_key;
  89. uint32_t auth_token;
  90. uint16_t msg_id;
  91. };
  92. #define DESKEY_LENGTH 28
  93. #define NEWCAMD_MSG_SIZE 400
  94. #define NEWCAMD_MAXPROV 32
  95. typedef struct {
  96. DES_key_schedule ks1;
  97. DES_key_schedule ks2;
  98. uint8_t des_key[16];
  99. } triple_des_t;
  100. struct newcamd {
  101. // newcamd private data
  102. uint8_t buf[NEWCAMD_MSG_SIZE];
  103. char hex_des_key[DESKEY_LENGTH + 1];
  104. uint8_t bin_des_key[DESKEY_LENGTH / 2]; // Decoded des_key
  105. triple_des_t td_key;
  106. uint16_t msg_id;
  107. // Initialized from CARD INFO command
  108. int caid;
  109. uint8_t ua[8];
  110. uint8_t num_of_provs;
  111. uint8_t provs_ident[NEWCAMD_MAXPROV][3];
  112. uint8_t provs_id[NEWCAMD_MAXPROV][8];
  113. uint8_t prov_ident_manual;
  114. char *crypt_passwd;
  115. };
  116. struct camd {
  117. int server_fd;
  118. struct in_addr server_addr;
  119. unsigned int server_port;
  120. char *user;
  121. char *pass;
  122. unsigned int ecm_recv_errors; // Error counter, reset on successful send/recv
  123. unsigned int emm_recv_errors; // Error counter, reset on successful send/recv
  124. struct key *key;
  125. unsigned int constant_codeword; // The codeword is set on the command line once, no ecm processing is done.
  126. pthread_t thread;
  127. QUEUE *req_queue;
  128. QUEUE *ecm_queue;
  129. QUEUE *emm_queue;
  130. struct camd_ops ops;
  131. struct cs378x cs378x;
  132. struct newcamd newcamd;
  133. };
  134. enum io_type {
  135. FILE_IO,
  136. NET_IO,
  137. WTF_IO
  138. };
  139. struct io {
  140. int fd;
  141. enum io_type type;
  142. char *fname;
  143. struct in_addr addr;
  144. unsigned int port;
  145. // Used only for output
  146. int ttl;
  147. int tos;
  148. struct in_addr intf;
  149. };
  150. struct packet_buf {
  151. int64_t time;
  152. uint8_t data[188];
  153. };
  154. #define MAX_PIDS 8192
  155. struct ts {
  156. // Stream handling
  157. struct ts_pat *pat, *curpat;
  158. struct ts_pat *genpat;
  159. uint8_t genpat_cc;
  160. struct ts_cat *cat, *curcat;
  161. struct ts_pmt *pmt, *curpmt;
  162. struct ts_sdt *sdt, *cursdt;
  163. struct ts_privsec *emm, *last_emm;
  164. struct ts_privsec *ecm, *last_ecm;
  165. struct ts_privsec *tmp_emm;
  166. struct ts_privsec *tmp_ecm;
  167. uint16_t pmt_pid;
  168. uint16_t service_id;
  169. uint16_t forced_service_id;
  170. uint16_t emm_caid, emm_pid;
  171. uint16_t ecm_caid, ecm_pid;
  172. uint16_t forced_caid;
  173. uint16_t forced_emm_pid;
  174. uint16_t forced_ecm_pid;
  175. pidmap_t pidmap;
  176. pidmap_t cc; // Continuity counters
  177. pidmap_t pid_seen;
  178. // Stats
  179. unsigned int emm_seen_count;
  180. unsigned int emm_processed_count;
  181. unsigned int emm_report_interval;
  182. time_t emm_last_report;
  183. unsigned int ecm_seen_count;
  184. unsigned int ecm_processed_count;
  185. unsigned int ecm_duplicate_count;
  186. unsigned int ecm_report_interval;
  187. time_t ecm_last_report;
  188. unsigned int cw_warn_sec;
  189. time_t cw_last_warn;
  190. time_t cw_next_warn;
  191. struct timeval ecm_change_time;
  192. unsigned int pid_report;
  193. unsigned int pid_stats[MAX_PIDS];
  194. // CAMD handling
  195. struct key key;
  196. struct camd camd;
  197. // Config
  198. char *ident;
  199. char *syslog_host;
  200. int syslog_port;
  201. int syslog_active;
  202. int syslog_remote;
  203. char *pidfile;
  204. enum CA_system req_CA_sys;
  205. int emm_send;
  206. int emm_only;
  207. int pid_filter;
  208. int eit_passthrough;
  209. int tdt_passthrough;
  210. int nit_passthrough;
  211. uint8_t irdeto_ecm;
  212. int ecm_cw_log;
  213. int rtp_input;
  214. int rtp_output;
  215. uint32_t rtp_ssrc;
  216. uint16_t rtp_seqnum;
  217. struct io input;
  218. struct io output;
  219. FILE *input_dump_file;
  220. char *input_dump_filename;
  221. int debug_level;
  222. int ts_discont;
  223. int camd_stop;
  224. int is_cw_error;
  225. int threaded;
  226. pthread_attr_t thread_attr;
  227. int decode_stop;
  228. pthread_t decode_thread;
  229. CBUF *decode_buf;
  230. int write_stop;
  231. pthread_t write_thread;
  232. CBUF *write_buf;
  233. struct notify *notify;
  234. char *notify_program;
  235. unsigned int input_buffer_time;
  236. LIST *input_buffer;
  237. };
  238. void data_init(struct ts *ts);
  239. void data_free(struct ts *ts);
  240. #endif