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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 (COPYING file) for more details.
  13. *
  14. */
  15. #ifndef DATA_H
  16. #define DATA_H
  17. #include <pthread.h>
  18. #include <limits.h>
  19. #include <stdbool.h>
  20. #include <openssl/aes.h>
  21. #include <openssl/des.h>
  22. #include <openssl/md5.h>
  23. #include "libfuncs/libfuncs.h"
  24. #include "libtsfuncs/tsfuncs.h"
  25. // 7 * 188
  26. #define FRAME_SIZE 1316
  27. // How much seconds to assume the key is valid
  28. #define KEY_VALID_TIME 10
  29. #define EMM_QUEUE_HARD_LIMIT 10000
  30. #define EMM_QUEUE_SOFT_LIMIT 1000
  31. #define ECM_QUEUE_HARD_LIMIT 10
  32. #define ECM_QUEUE_SOFT_LIMIT 3
  33. // 64k should be enough for everybody
  34. #define THREAD_STACK_SIZE (64 * 1024)
  35. struct notify {
  36. pthread_t thread; /* Thread handle */
  37. QUEUE *notifications; /* Notification queue */
  38. char ident[512]; /* tsdecrypt ident (set by -i) */
  39. char program[512]; /* What program to exec */
  40. };
  41. #define CODEWORD_LENGTH 16
  42. #define BISSKEY_LENGTH 6
  43. typedef void csakey_t;
  44. struct key {
  45. uint8_t cw[CODEWORD_LENGTH];
  46. csakey_t *csakey;
  47. int is_valid_cw;
  48. time_t ts; // At what time the key is set
  49. struct timeval ts_keyset; // At what time the key is set
  50. };
  51. // 4 auth header, 20 header size, 256 max data size, 16 potential padding
  52. #define CAMD35_HDR_LEN (20)
  53. #define CAMD35_BUF_LEN (4 + CAMD35_HDR_LEN + 256 + 16)
  54. // When this limit is reached invalid_cw flag is set.
  55. #define ECM_RECV_ERRORS_LIMIT 10
  56. // When this limit is reached camd_reconnect is called.
  57. #define EMM_RECV_ERRORS_LIMIT 100
  58. struct camd;
  59. struct ts;
  60. enum msg_type { EMM_MSG, ECM_MSG };
  61. struct camd_msg {
  62. enum msg_type type;
  63. uint16_t ca_id;
  64. uint16_t service_id;
  65. uint8_t data_len;
  66. uint8_t data[255];
  67. struct ts *ts;
  68. };
  69. enum camd_proto {
  70. CAMD_CS378X,
  71. CAMD_NEWCAMD,
  72. };
  73. struct camd_ops {
  74. char *ident;
  75. enum camd_proto proto;
  76. int (*connect)(struct camd *c);
  77. void (*disconnect)(struct camd *c);
  78. int (*reconnect)(struct camd *c);
  79. int (*do_emm)(struct camd *c, struct camd_msg *msg);
  80. int (*do_ecm)(struct camd *c, struct camd_msg *msg);
  81. int (*get_cw)(struct camd *c, uint16_t *ca_id, uint16_t *idx, uint8_t *cw);
  82. };
  83. struct cs378x {
  84. // cs378x private data
  85. uint8_t buf[CAMD35_BUF_LEN];
  86. AES_KEY aes_encrypt_key;
  87. AES_KEY aes_decrypt_key;
  88. uint32_t auth_token;
  89. uint16_t msg_id;
  90. };
  91. #define DESKEY_LENGTH 28
  92. #define NEWCAMD_MSG_SIZE 400
  93. #define NEWCAMD_MAXPROV 32
  94. typedef struct {
  95. DES_key_schedule ks1;
  96. DES_key_schedule ks2;
  97. uint8_t des_key[16];
  98. } triple_des_t;
  99. struct newcamd {
  100. // newcamd private data
  101. uint8_t buf[NEWCAMD_MSG_SIZE];
  102. char hex_des_key[DESKEY_LENGTH + 1];
  103. uint8_t bin_des_key[DESKEY_LENGTH / 2]; // Decoded des_key
  104. triple_des_t td_key;
  105. uint16_t msg_id;
  106. // Initialized from CARD INFO command
  107. int caid;
  108. uint8_t ua[8];
  109. uint8_t num_of_provs;
  110. uint8_t provs_ident[NEWCAMD_MAXPROV][3];
  111. uint8_t provs_id[NEWCAMD_MAXPROV][8];
  112. uint8_t prov_ident_manual;
  113. char *crypt_passwd;
  114. };
  115. struct camd {
  116. int server_fd;
  117. char *hostname;
  118. char *service;
  119. char *user;
  120. char *pass;
  121. unsigned int ecm_recv_errors; // Error counter, reset on successful send/recv
  122. unsigned int emm_recv_errors; // Error counter, reset on successful send/recv
  123. unsigned int no_reconnect;
  124. unsigned int check_emm_errors;
  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. char *hostname;
  145. char *service;
  146. // Used only for output
  147. int ttl;
  148. int tos;
  149. struct in_addr intf;
  150. int v6_if_index;
  151. // Used for input
  152. struct in_addr isrc;
  153. };
  154. struct packet_buf {
  155. int64_t time;
  156. uint8_t data[188];
  157. };
  158. #define MAX_FILTERS 16
  159. #define MAX_FILTER_NAME 32
  160. #define MAX_FILTER_LEN 16
  161. enum filter_action {
  162. FILTER_NO_MATCH = 0,
  163. FILTER_ACCEPT_ALL = 1,
  164. FILTER_REJECT_ALL = 2,
  165. FILTER_ACCEPT = 3,
  166. FILTER_REJECT = 4,
  167. };
  168. enum filter_type {
  169. FILTER_TYPE_DATA = 0, // Compare data at offset X
  170. FILTER_TYPE_MASK = 1, // Compare data + mask
  171. FILTER_TYPE_LENGTH = 2, // Compare section length /EMM[2]/)
  172. };
  173. struct filter {
  174. enum filter_action action;
  175. enum filter_type type;
  176. uint8_t offset; // Offset into EMM
  177. uint8_t filter_len; // Filter length
  178. uint8_t data[MAX_FILTER_LEN]; // Data | Matched bytes
  179. uint8_t mask[MAX_FILTER_LEN]; // Mask bytes
  180. char name[MAX_FILTER_NAME]; // Filter name (default: NO_NAME)
  181. };
  182. #define MAX_PIDS 8192
  183. struct ts {
  184. // Stream handling
  185. struct ts_pat *pat, *curpat;
  186. struct ts_pat *genpat;
  187. uint8_t genpat_cc;
  188. struct ts_cat *cat, *curcat;
  189. struct ts_pmt *pmt, *curpmt;
  190. struct ts_sdt *sdt, *cursdt;
  191. struct ts_privsec *emm, *last_emm;
  192. struct ts_privsec *ecm, *last_ecm;
  193. struct ts_privsec *tmp_emm;
  194. struct ts_privsec *tmp_ecm;
  195. uint16_t pmt_pid;
  196. uint16_t service_id;
  197. uint16_t forced_service_id;
  198. uint16_t emm_caid, emm_pid;
  199. uint16_t ecm_caid, ecm_pid;
  200. uint16_t forced_caid;
  201. uint16_t forced_emm_pid;
  202. uint16_t forced_ecm_pid;
  203. pidmap_t pidmap;
  204. pidmap_t cc; // Continuity counters
  205. pidmap_t pid_seen;
  206. // Stats
  207. unsigned int emm_input_count;
  208. unsigned int emm_seen_count;
  209. unsigned int emm_processed_count;
  210. unsigned int emm_skipped_count;
  211. unsigned int emm_report_interval;
  212. time_t emm_last_report;
  213. unsigned int ecm_seen_count;
  214. unsigned int ecm_processed_count;
  215. unsigned int ecm_duplicate_count;
  216. unsigned int ecm_report_interval;
  217. time_t ecm_last_report;
  218. unsigned int cw_warn_sec;
  219. time_t cw_last_warn;
  220. time_t cw_next_warn;
  221. struct timeval ecm_change_time;
  222. unsigned int pid_report;
  223. unsigned int pid_stats[MAX_PIDS];
  224. // CAMD handling
  225. struct key key;
  226. struct camd camd;
  227. // Config
  228. char *ident;
  229. char *syslog_host;
  230. int syslog_port;
  231. int syslog_active;
  232. int syslog_remote;
  233. char *pidfile;
  234. enum CA_system req_CA_sys;
  235. bool output_stream; // Decode and output the decoded stream
  236. bool process_ecm; // Process ECM packets (and send them to CAMD)
  237. bool process_emm; // Process EMM packets (and send them to CAMD)
  238. int pid_filter;
  239. int eit_passthrough;
  240. int tdt_passthrough;
  241. int nit_passthrough;
  242. uint8_t irdeto_ecm;
  243. int ecm_cw_log;
  244. int rtp_input;
  245. int rtp_output;
  246. uint32_t rtp_ssrc;
  247. uint16_t rtp_seqnum;
  248. struct io input;
  249. struct io output;
  250. FILE *input_dump_file;
  251. char *input_dump_filename;
  252. int debug_level;
  253. int ts_discont;
  254. int camd_stop;
  255. int is_cw_error;
  256. int no_output_on_error;
  257. int threaded;
  258. pthread_attr_t thread_attr;
  259. int decode_stop;
  260. pthread_t decode_thread;
  261. CBUF *decode_buf;
  262. int write_stop;
  263. pthread_t write_thread;
  264. CBUF *write_buf;
  265. struct notify *notify;
  266. char *notify_program;
  267. unsigned int input_buffer_time;
  268. LIST *input_buffer;
  269. int emm_filters_num;
  270. struct filter emm_filters[MAX_FILTERS];
  271. };
  272. void data_init(struct ts *ts);
  273. void data_free(struct ts *ts);
  274. #endif