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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. // Prevent warnings about openssl functions. Apple may consider 'openssl'
  21. // deprecated but changing perfectly working portable code just because they
  22. // introduced some proprietary API is not going to happen.
  23. #if defined(__APPLE__)
  24. #define __AVAILABILITY_MACROS_USES_AVAILABILITY 0
  25. #define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6
  26. #endif
  27. #include <openssl/aes.h>
  28. #include <openssl/des.h>
  29. #include <openssl/md5.h>
  30. #include <openssl/rand.h>
  31. #include "libfuncs/libfuncs.h"
  32. #include "libtsfuncs/tsfuncs.h"
  33. // 7 * 188
  34. #define FRAME_SIZE 1316
  35. // How much seconds to assume the key is valid
  36. #define KEY_VALID_TIME 30
  37. #define EMM_QUEUE_HARD_LIMIT 10000
  38. #define EMM_QUEUE_SOFT_LIMIT 1000
  39. #define ECM_QUEUE_HARD_LIMIT 10
  40. #define ECM_QUEUE_SOFT_LIMIT 3
  41. // 64k should be enough for everybody
  42. #define THREAD_STACK_SIZE (64 * 1024)
  43. struct notify {
  44. pthread_t thread; /* Thread handle */
  45. QUEUE *notifications; /* Notification queue */
  46. char ident[512]; /* tsdecrypt ident (set by -i) */
  47. char program[512]; /* What program to exec */
  48. };
  49. #define CODEWORD_LENGTH 16
  50. #define BISSKEY_LENGTH 6
  51. typedef void csakey_t;
  52. struct key {
  53. uint8_t cw[CODEWORD_LENGTH];
  54. csakey_t *csakey;
  55. int is_valid_cw;
  56. time_t ts; // At what time the key is set
  57. struct timeval ts_keyset; // At what time the key is set
  58. };
  59. // 4 auth header, 20 header size, 256 max data size, 16 potential padding
  60. #define CAMD35_HDR_LEN (20)
  61. #define CAMD35_DATA_SIZE (256)
  62. #define CAMD35_BUF_LEN (4 + CAMD35_HDR_LEN + CAMD35_DATA_SIZE + 16)
  63. // When this limit is reached invalid_cw flag is set.
  64. #define ECM_RECV_ERRORS_LIMIT 10
  65. // When this limit is reached camd_reconnect is called.
  66. #define EMM_RECV_ERRORS_LIMIT 100
  67. struct camd;
  68. struct ts;
  69. enum msg_type { EMM_MSG, ECM_MSG };
  70. struct camd_msg {
  71. enum msg_type type;
  72. uint16_t ca_id;
  73. uint16_t service_id;
  74. int data_len;
  75. uint8_t data[CAMD35_BUF_LEN];
  76. struct ts *ts;
  77. };
  78. enum camd_proto {
  79. CAMD_CS378X,
  80. CAMD_NEWCAMD,
  81. };
  82. struct camd_ops {
  83. char *ident;
  84. enum camd_proto proto;
  85. int (*connect)(struct camd *c);
  86. void (*disconnect)(struct camd *c);
  87. int (*reconnect)(struct camd *c);
  88. int (*do_emm)(struct camd *c, struct camd_msg *msg);
  89. int (*do_ecm)(struct camd *c, struct camd_msg *msg);
  90. int (*get_cw)(struct camd *c, uint16_t *ca_id, uint16_t *idx, uint8_t *cw);
  91. };
  92. struct cs378x {
  93. // cs378x private data
  94. uint8_t buf[CAMD35_BUF_LEN];
  95. AES_KEY aes_encrypt_key;
  96. AES_KEY aes_decrypt_key;
  97. uint32_t auth_token;
  98. uint16_t msg_id;
  99. };
  100. #define DESKEY_LENGTH 28
  101. #define NEWCAMD_MSG_SIZE 400
  102. #define NEWCAMD_MAXPROV 32
  103. typedef struct {
  104. DES_key_schedule ks1;
  105. DES_key_schedule ks2;
  106. uint8_t des_key[16];
  107. } triple_des_t;
  108. struct newcamd {
  109. // newcamd private data
  110. uint8_t buf[NEWCAMD_MSG_SIZE];
  111. char hex_des_key[DESKEY_LENGTH + 1];
  112. uint8_t bin_des_key[DESKEY_LENGTH / 2]; // Decoded des_key
  113. triple_des_t td_key;
  114. uint16_t msg_id;
  115. // Initialized from CARD INFO command
  116. int caid;
  117. uint8_t ua[8];
  118. uint8_t num_of_provs;
  119. uint8_t provs_ident[NEWCAMD_MAXPROV][3];
  120. uint8_t provs_id[NEWCAMD_MAXPROV][8];
  121. uint8_t prov_ident_manual;
  122. char *crypt_passwd;
  123. };
  124. struct camd {
  125. int server_fd;
  126. char *hostname;
  127. char *service;
  128. char *user;
  129. char *pass;
  130. unsigned int ecm_recv_errors; // Error counter, reset on successful send/recv
  131. unsigned int emm_recv_errors; // Error counter, reset on successful send/recv
  132. unsigned int no_reconnect;
  133. unsigned int check_emm_errors;
  134. struct key *key;
  135. unsigned int constant_codeword; // The codeword is set on the command line once, no ecm processing is done.
  136. pthread_t thread;
  137. QUEUE *req_queue;
  138. QUEUE *ecm_queue;
  139. QUEUE *emm_queue;
  140. struct camd_ops ops;
  141. struct cs378x cs378x;
  142. struct newcamd newcamd;
  143. };
  144. enum io_type {
  145. FILE_IO,
  146. NET_IO,
  147. WTF_IO
  148. };
  149. struct io {
  150. int fd;
  151. enum io_type type;
  152. char *fname;
  153. char *hostname;
  154. char *service;
  155. // Used only for output
  156. int ttl;
  157. int tos;
  158. struct in_addr intf;
  159. int v6_if_index;
  160. // Used for input
  161. struct in_addr isrc;
  162. };
  163. struct packet_buf {
  164. int64_t time;
  165. uint8_t data[188];
  166. };
  167. #define MAX_FILTERS 16
  168. #define MAX_FILTER_NAME 32
  169. #define MAX_FILTER_LEN 16
  170. enum filter_action {
  171. FILTER_NO_MATCH = 0,
  172. FILTER_ACCEPT_ALL = 1,
  173. FILTER_REJECT_ALL = 2,
  174. FILTER_ACCEPT = 3,
  175. FILTER_REJECT = 4,
  176. };
  177. enum filter_type {
  178. FILTER_TYPE_DATA = 0, // Compare data at offset X
  179. FILTER_TYPE_MASK = 1, // Compare data + mask
  180. FILTER_TYPE_LENGTH = 2, // Compare section length /EMM[2]/)
  181. };
  182. struct filter {
  183. enum filter_action action;
  184. enum filter_type type;
  185. uint8_t offset; // Offset into EMM
  186. uint8_t filter_len; // Filter length
  187. uint8_t data[MAX_FILTER_LEN]; // Data | Matched bytes
  188. uint8_t mask[MAX_FILTER_LEN]; // Mask bytes
  189. char name[MAX_FILTER_NAME]; // Filter name (default: NO_NAME)
  190. };
  191. struct chid {
  192. int seen;
  193. uint16_t chid;
  194. };
  195. #define MAX_PIDS 8192
  196. #define MAX_ECM_PIDS 16
  197. struct ts {
  198. // Stream handling
  199. struct ts_pat *pat, *curpat;
  200. struct ts_pat *genpat;
  201. uint8_t genpat_cc;
  202. struct ts_cat *cat, *curcat;
  203. struct ts_pmt *pmt, *curpmt;
  204. struct ts_sdt *sdt, *cursdt;
  205. struct ts_privsec *emm, *last_emm;
  206. struct ts_privsec *ecm, *last_ecm;
  207. struct ts_privsec *tmp_emm;
  208. struct ts_privsec *tmp_ecm;
  209. uint16_t pmt_pid;
  210. uint16_t service_id;
  211. uint16_t forced_service_id;
  212. uint16_t emm_caid, emm_pid;
  213. uint16_t ecm_caid, ecm_pid;
  214. unsigned int n_ecm_pids;
  215. unsigned int ecm_pid_idx;
  216. uint16_t ecm_pids[MAX_ECM_PIDS];
  217. uint16_t forced_caid;
  218. uint16_t forced_emm_pid;
  219. uint16_t forced_ecm_pid;
  220. pidmap_t pidmap;
  221. pidmap_t cc; // Continuity counters
  222. pidmap_t pid_seen;
  223. // PMT tracking
  224. time_t last_pmt_ts;
  225. unsigned int have_valid_pmt;
  226. unsigned int no_input;
  227. // Stats
  228. unsigned int emm_input_count;
  229. unsigned int emm_seen_count;
  230. unsigned int emm_processed_count;
  231. unsigned int emm_skipped_count;
  232. unsigned int emm_report_interval;
  233. time_t emm_last_report;
  234. unsigned int ecm_seen_count;
  235. unsigned int ecm_processed_count;
  236. unsigned int ecm_duplicate_count;
  237. unsigned int ecm_report_interval;
  238. time_t ecm_last_report;
  239. unsigned int cw_warn_sec;
  240. time_t cw_last_warn;
  241. time_t cw_next_warn;
  242. struct timeval ecm_change_time;
  243. unsigned int stream_is_not_scrambled;
  244. time_t last_scrambled_packet_ts;
  245. time_t last_not_scrambled_packet_ts;
  246. time_t last_not_scrambled_report_ts;
  247. bool allow_encrypted_output;
  248. bool output_is_encrypted;
  249. int64_t last_encrypted_output_ts;
  250. int64_t last_decrypted_output_ts;
  251. unsigned int pid_report;
  252. unsigned int pid_stats[MAX_PIDS];
  253. // CAMD handling
  254. struct key key;
  255. struct camd camd;
  256. // Config
  257. char *ident;
  258. char *syslog_host;
  259. int syslog_port;
  260. int syslog_active;
  261. int syslog_remote;
  262. char *pidfile;
  263. enum CA_system req_CA_sys;
  264. bool output_stream; // Decode and output the decoded stream
  265. bool process_ecm; // Process ECM packets (and send them to CAMD)
  266. bool process_emm; // Process EMM packets (and send them to CAMD)
  267. int pid_filter;
  268. int eit_passthrough;
  269. int tdt_passthrough;
  270. int nit_passthrough;
  271. uint8_t irdeto_ecm_idx;
  272. uint16_t irdeto_ecm_chid;
  273. enum {
  274. IRDETO_FILTER_IDX,
  275. IRDETO_FILTER_CHID,
  276. } irdeto_ecm_filter_type;
  277. struct chid irdeto_chid[0xff];
  278. uint8_t irdeto_max_chids;
  279. int ecm_cw_log;
  280. int rtp_input;
  281. int rtp_output;
  282. uint32_t rtp_ssrc;
  283. uint16_t rtp_seqnum;
  284. struct io input;
  285. struct io output;
  286. FILE *input_dump_file;
  287. char *input_dump_filename;
  288. int debug_level;
  289. int ts_discont;
  290. int camd_stop;
  291. int is_cw_error;
  292. int no_output_on_error;
  293. int threaded;
  294. pthread_attr_t thread_attr;
  295. int decode_stop;
  296. pthread_t decode_thread;
  297. CBUF *decode_buf;
  298. int write_stop;
  299. pthread_t write_thread;
  300. CBUF *write_buf;
  301. struct notify *notify;
  302. char *notify_program;
  303. int notify_wait;
  304. char *status_file;
  305. char *status_file_tmp;
  306. unsigned int input_buffer_time;
  307. LIST *input_buffer;
  308. int emm_filters_num;
  309. struct filter emm_filters[MAX_FILTERS];
  310. };
  311. void data_init(struct ts *ts);
  312. void data_free(struct ts *ts);
  313. #endif