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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. };
  152. struct packet_buf {
  153. int64_t time;
  154. uint8_t data[188];
  155. };
  156. #define MAX_FILTERS 16
  157. #define MAX_FILTER_NAME 32
  158. #define MAX_FILTER_LEN 16
  159. enum filter_action {
  160. FILTER_NO_MATCH = 0,
  161. FILTER_ACCEPT_ALL = 1,
  162. FILTER_REJECT_ALL = 2,
  163. FILTER_ACCEPT = 3,
  164. FILTER_REJECT = 4,
  165. };
  166. struct filter {
  167. enum filter_action action; // If set to 1, the filter is negative else the filter is positive
  168. uint8_t offset; // Offset into EMM
  169. uint8_t data_len; // Filter length
  170. uint8_t data[MAX_FILTER_LEN]; // Filter bytes
  171. char name[MAX_FILTER_NAME]; // Filter name (default: NO_NAME)
  172. };
  173. #define MAX_PIDS 8192
  174. struct ts {
  175. // Stream handling
  176. struct ts_pat *pat, *curpat;
  177. struct ts_pat *genpat;
  178. uint8_t genpat_cc;
  179. struct ts_cat *cat, *curcat;
  180. struct ts_pmt *pmt, *curpmt;
  181. struct ts_sdt *sdt, *cursdt;
  182. struct ts_privsec *emm, *last_emm;
  183. struct ts_privsec *ecm, *last_ecm;
  184. struct ts_privsec *tmp_emm;
  185. struct ts_privsec *tmp_ecm;
  186. uint16_t pmt_pid;
  187. uint16_t service_id;
  188. uint16_t forced_service_id;
  189. uint16_t emm_caid, emm_pid;
  190. uint16_t ecm_caid, ecm_pid;
  191. uint16_t forced_caid;
  192. uint16_t forced_emm_pid;
  193. uint16_t forced_ecm_pid;
  194. pidmap_t pidmap;
  195. pidmap_t cc; // Continuity counters
  196. pidmap_t pid_seen;
  197. // Stats
  198. unsigned int emm_input_count;
  199. unsigned int emm_seen_count;
  200. unsigned int emm_processed_count;
  201. unsigned int emm_skipped_count;
  202. unsigned int emm_report_interval;
  203. time_t emm_last_report;
  204. unsigned int ecm_seen_count;
  205. unsigned int ecm_processed_count;
  206. unsigned int ecm_duplicate_count;
  207. unsigned int ecm_report_interval;
  208. time_t ecm_last_report;
  209. unsigned int cw_warn_sec;
  210. time_t cw_last_warn;
  211. time_t cw_next_warn;
  212. struct timeval ecm_change_time;
  213. unsigned int pid_report;
  214. unsigned int pid_stats[MAX_PIDS];
  215. // CAMD handling
  216. struct key key;
  217. struct camd camd;
  218. // Config
  219. char *ident;
  220. char *syslog_host;
  221. int syslog_port;
  222. int syslog_active;
  223. int syslog_remote;
  224. char *pidfile;
  225. enum CA_system req_CA_sys;
  226. bool output_stream; // Decode and output the decoded stream
  227. bool process_ecm; // Process ECM packets (and send them to CAMD)
  228. bool process_emm; // Process EMM packets (and send them to CAMD)
  229. int pid_filter;
  230. int eit_passthrough;
  231. int tdt_passthrough;
  232. int nit_passthrough;
  233. uint8_t irdeto_ecm;
  234. int ecm_cw_log;
  235. int rtp_input;
  236. int rtp_output;
  237. uint32_t rtp_ssrc;
  238. uint16_t rtp_seqnum;
  239. struct io input;
  240. struct io output;
  241. FILE *input_dump_file;
  242. char *input_dump_filename;
  243. int debug_level;
  244. int ts_discont;
  245. int camd_stop;
  246. int is_cw_error;
  247. int no_output_on_error;
  248. int threaded;
  249. pthread_attr_t thread_attr;
  250. int decode_stop;
  251. pthread_t decode_thread;
  252. CBUF *decode_buf;
  253. int write_stop;
  254. pthread_t write_thread;
  255. CBUF *write_buf;
  256. struct notify *notify;
  257. char *notify_program;
  258. unsigned int input_buffer_time;
  259. LIST *input_buffer;
  260. int emm_filters_num;
  261. struct filter emm_filters[MAX_FILTERS];
  262. };
  263. void data_init(struct ts *ts);
  264. void data_free(struct ts *ts);
  265. #endif