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

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