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

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