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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <openssl/aes.h>
  20. #include <openssl/des.h>
  21. #include <openssl/md5.h>
  22. #include "libfuncs/libfuncs.h"
  23. #include "libtsfuncs/tsfuncs.h"
  24. // 7 * 188
  25. #define FRAME_SIZE 1316
  26. // How much seconds to assume the key is valid
  27. #define KEY_VALID_TIME 10
  28. #define EMM_QUEUE_HARD_LIMIT 10000
  29. #define EMM_QUEUE_SOFT_LIMIT 1000
  30. #define ECM_QUEUE_HARD_LIMIT 10
  31. #define ECM_QUEUE_SOFT_LIMIT 3
  32. // 64k should be enough for everybody
  33. #define THREAD_STACK_SIZE (64 * 1024)
  34. struct notify {
  35. pthread_t thread; /* Thread handle */
  36. QUEUE *notifications; /* Notification queue */
  37. char ident[512]; /* tsdecrypt ident (set by -i) */
  38. char program[512]; /* What program to exec */
  39. };
  40. #define CODEWORD_LENGTH 16
  41. #define BISSKEY_LENGTH 6
  42. typedef void csakey_t;
  43. struct key {
  44. uint8_t cw[CODEWORD_LENGTH];
  45. csakey_t *csakey;
  46. int is_valid_cw;
  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;
  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. char *crypt_passwd;
  113. };
  114. struct camd {
  115. int server_fd;
  116. struct in_addr server_addr;
  117. unsigned int server_port;
  118. char *user;
  119. char *pass;
  120. unsigned int ecm_recv_errors; // Error counter, reset on successful send/recv
  121. unsigned int emm_recv_errors; // Error counter, reset on successful send/recv
  122. struct key *key;
  123. unsigned int constant_codeword; // The codeword is set on the command line once, no ecm processing is done.
  124. pthread_t thread;
  125. QUEUE *req_queue;
  126. QUEUE *ecm_queue;
  127. QUEUE *emm_queue;
  128. struct camd_ops ops;
  129. struct cs378x cs378x;
  130. struct newcamd newcamd;
  131. };
  132. enum io_type {
  133. FILE_IO,
  134. NET_IO,
  135. WTF_IO
  136. };
  137. struct io {
  138. int fd;
  139. enum io_type type;
  140. char *fname;
  141. struct in_addr addr;
  142. unsigned int port;
  143. // Used only for output
  144. int ttl;
  145. int tos;
  146. struct in_addr intf;
  147. };
  148. struct packet_buf {
  149. int64_t time;
  150. uint8_t data[188];
  151. };
  152. #define MAX_PIDS 8192
  153. struct ts {
  154. // Stream handling
  155. struct ts_pat *pat, *curpat;
  156. struct ts_pat *genpat;
  157. uint8_t genpat_cc;
  158. struct ts_cat *cat, *curcat;
  159. struct ts_pmt *pmt, *curpmt;
  160. struct ts_sdt *sdt, *cursdt;
  161. struct ts_privsec *emm, *last_emm;
  162. struct ts_privsec *ecm, *last_ecm;
  163. struct ts_privsec *tmp_emm;
  164. struct ts_privsec *tmp_ecm;
  165. uint16_t pmt_pid;
  166. uint16_t service_id;
  167. uint16_t forced_service_id;
  168. uint16_t emm_caid, emm_pid;
  169. uint16_t ecm_caid, ecm_pid;
  170. uint16_t forced_caid;
  171. uint16_t forced_emm_pid;
  172. uint16_t forced_ecm_pid;
  173. pidmap_t pidmap;
  174. pidmap_t cc; // Continuity counters
  175. pidmap_t pid_seen;
  176. // Stats
  177. unsigned int emm_seen_count;
  178. unsigned int emm_processed_count;
  179. unsigned int emm_report_interval;
  180. time_t emm_last_report;
  181. unsigned int ecm_seen_count;
  182. unsigned int ecm_processed_count;
  183. unsigned int ecm_duplicate_count;
  184. unsigned int ecm_report_interval;
  185. time_t ecm_last_report;
  186. unsigned int cw_warn_sec;
  187. time_t cw_last_warn;
  188. time_t cw_next_warn;
  189. struct timeval ecm_change_time;
  190. unsigned int pid_report;
  191. unsigned int pid_stats[MAX_PIDS];
  192. // CAMD handling
  193. struct key key;
  194. struct camd camd;
  195. // Config
  196. char *ident;
  197. char *syslog_host;
  198. int syslog_port;
  199. int syslog_active;
  200. int syslog_remote;
  201. char *pidfile;
  202. enum CA_system req_CA_sys;
  203. int emm_send;
  204. int emm_only;
  205. int pid_filter;
  206. int eit_passthrough;
  207. int tdt_passthrough;
  208. int nit_passthrough;
  209. uint8_t irdeto_ecm;
  210. int ecm_cw_log;
  211. int rtp_input;
  212. int rtp_output;
  213. uint32_t rtp_ssrc;
  214. uint16_t rtp_seqnum;
  215. struct io input;
  216. struct io output;
  217. FILE *input_dump_file;
  218. char *input_dump_filename;
  219. int debug_level;
  220. int ts_discont;
  221. int camd_stop;
  222. int is_cw_error;
  223. int no_output_on_error;
  224. int threaded;
  225. pthread_attr_t thread_attr;
  226. int decode_stop;
  227. pthread_t decode_thread;
  228. CBUF *decode_buf;
  229. int write_stop;
  230. pthread_t write_thread;
  231. CBUF *write_buf;
  232. struct notify *notify;
  233. char *notify_program;
  234. unsigned int input_buffer_time;
  235. LIST *input_buffer;
  236. };
  237. void data_init(struct ts *ts);
  238. void data_free(struct ts *ts);
  239. #endif