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

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