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.

camd.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * CAMD communications
  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. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <sys/errno.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <netinet/tcp.h>
  25. #include <arpa/inet.h>
  26. #include <openssl/aes.h>
  27. #include <openssl/md5.h>
  28. #include <dvbcsa/dvbcsa.h>
  29. #include "libfuncs/libfuncs.h"
  30. #include "libtsfuncs/tsfuncs.h"
  31. #include "data.h"
  32. #include "util.h"
  33. #include "camd.h"
  34. #include "notify.h"
  35. static int connect_to(struct in_addr ip, int port) {
  36. ts_LOGf("CAM | Connecting to server %s:%d\n", inet_ntoa(ip), port);
  37. int fd = socket(PF_INET, SOCK_STREAM, 0);
  38. if (fd < 0) {
  39. ts_LOGf("CAM | Could not create socket | %s\n", strerror(errno));
  40. return -1;
  41. }
  42. struct sockaddr_in sock;
  43. sock.sin_family = AF_INET;
  44. sock.sin_port = htons(port);
  45. sock.sin_addr = ip;
  46. if (do_connect(fd, (struct sockaddr *)&sock, sizeof(sock), 1000) < 0) {
  47. ts_LOGf("CAM | Could not connect to server %s:%d | %s\n", inet_ntoa(ip), port, strerror(errno));
  48. close(fd);
  49. sleep(1);
  50. return -1;
  51. }
  52. int flag = 1;
  53. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
  54. ts_LOGf("CAM | Connected to fd:%d\n", fd);
  55. return fd;
  56. }
  57. static void camd35_init_auth(struct ts *ts) {
  58. struct camd35 *c = &ts->camd35;
  59. unsigned char dump[16];
  60. if (c->auth_token)
  61. return;
  62. c->auth_token = crc32(0L, MD5((unsigned char *)c->user, strlen(c->user), dump), 16);
  63. MD5((unsigned char *)c->pass, strlen(c->pass), dump);
  64. AES_set_encrypt_key(dump, 128, &c->aes_encrypt_key);
  65. AES_set_decrypt_key(dump, 128, &c->aes_decrypt_key);
  66. }
  67. static int camd35_connect(struct ts *ts) {
  68. struct camd35 *c = &ts->camd35;
  69. if (c->server_fd < 0)
  70. c->server_fd = connect_to(c->server_addr, c->server_port);
  71. return c->server_fd;
  72. }
  73. static void camd35_disconnect(struct ts *ts) {
  74. struct camd35 *c = &ts->camd35;
  75. shutdown_fd(&c->server_fd);
  76. }
  77. static int camd35_reconnect(struct ts *ts) {
  78. camd35_disconnect(ts);
  79. return camd35_connect(ts);
  80. }
  81. static int camd35_recv(struct camd35 *c, uint8_t *data, int *data_len) {
  82. int i;
  83. // Read AUTH token
  84. ssize_t r = fdread(c->server_fd, (char *)data, 4);
  85. if (r < 4)
  86. return -1;
  87. uint32_t auth_token = (((data[0] << 24) | (data[1] << 16) | (data[2]<<8) | data[3]) & 0xffffffffL);
  88. if (auth_token != c->auth_token)
  89. ts_LOGf("WARN: recv auth 0x%08x != camd35_auth 0x%08x\n", auth_token, c->auth_token);
  90. *data_len = 256;
  91. for (i = 0; i < *data_len; i += 16) { // Read and decrypt payload
  92. fdread(c->server_fd, (char *)data + i, 16);
  93. AES_decrypt(data + i, data + i, &c->aes_decrypt_key);
  94. if (i == 0)
  95. *data_len = boundary(4, data[1] + 20); // Initialize real data length
  96. }
  97. return *data_len;
  98. }
  99. static int camd35_recv_cw(struct ts *ts) {
  100. struct camd35 *c = &ts->camd35;
  101. struct timeval tv1, tv2, last_ts_keyset;
  102. static uint8_t invalid_cw[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  103. uint8_t *data = c->buf;
  104. int data_len = 0;
  105. int ret = 0;
  106. gettimeofday(&tv1, NULL);
  107. READ:
  108. ret = camd35_recv(c, data, &data_len);
  109. if (ret < 0) {
  110. ts_LOGf("ERR | No code word has been received (ret = %d)\n", ret);
  111. camd35_reconnect(ts);
  112. return ret;
  113. }
  114. // EMM request, ignore it. Sometimes OSCAM sends two EMM requests after CW
  115. if (data[0] == 0x05)
  116. goto READ;
  117. if (data[0] != 0x01) {
  118. ts_LOGf("ERR | Unexpected server response on code word request (ret data[0] == 0x%02x /%s/)\n",
  119. data[0],
  120. data[0] == 0x08 ? "No card" :
  121. data[0] == 0x44 ? "No code word found" : "Unknown err");
  122. c->ecm_recv_errors++;
  123. usleep(10000);
  124. if (c->ecm_recv_errors >= ECM_RECV_ERRORS_LIMIT) {
  125. c->key->is_valid_cw = 0;
  126. memcpy(c->key->cw, invalid_cw, 16);
  127. }
  128. return 0;
  129. }
  130. if (data_len < 48) {
  131. ts_LOGf("ERR | Code word data_len (%d) mismatch != 48\n", data_len);
  132. return 0;
  133. }
  134. if (data[1] < 0x10) {
  135. ts_LOGf("ERR | Code word len (%d) mismatch != 16\n", data[1]);
  136. return 0;
  137. }
  138. gettimeofday(&tv2, NULL);
  139. uint16_t ca_id = (data[10] << 8) | data[11];
  140. uint16_t idx = (data[16] << 8) | data[17];
  141. uint8_t *cw = data + 20;
  142. memcpy(c->key->cw, cw, 16);
  143. char cw_dump[16 * 6];
  144. ts_hex_dump_buf(cw_dump, 16 * 6, cw, 16, 0);
  145. int valid_cw = memcmp(c->key->cw, invalid_cw, 16) != 0;
  146. if (!c->key->is_valid_cw && valid_cw) {
  147. ts_LOGf("CW | OK: Valid code word was received.\n");
  148. notify(ts, "CODE_WORD_OK", "Valid code word was received.");
  149. }
  150. c->key->is_valid_cw = valid_cw;
  151. // At first ts_keyset is not initialized
  152. last_ts_keyset = c->key->ts_keyset;
  153. if (c->key->is_valid_cw) {
  154. c->ecm_recv_errors = 0;
  155. gettimeofday(&c->key->ts_keyset, NULL);
  156. c->key->ts = c->key->ts_keyset.tv_sec;
  157. ts->cw_last_warn = c->key->ts;
  158. if (memcmp(c->key->cw, invalid_cw, 8) != 0) {
  159. dvbcsa_key_set (c->key->cw, c->key->csakey[0]);
  160. dvbcsa_bs_key_set(c->key->cw, c->key->bs_csakey[0]);
  161. }
  162. if (memcmp(c->key->cw + 8, invalid_cw, 8) != 0) {
  163. dvbcsa_key_set(c->key->cw + 8, c->key->csakey[1]);
  164. dvbcsa_bs_key_set(c->key->cw + 8, c->key->bs_csakey[1]);
  165. }
  166. }
  167. if (ts->ecm_cw_log) {
  168. ts_LOGf("CW | CAID: 0x%04x [ %5llu ms ] ( %6llu ms ) ------- IDX: 0x%04x Data: %s\n",
  169. ca_id, timeval_diff_msec(&tv1, &tv2),
  170. timeval_diff_msec(&last_ts_keyset, &tv2),
  171. idx, cw_dump );
  172. }
  173. return ret;
  174. }
  175. #undef ERR
  176. static int camd35_send_buf(struct ts *ts, int data_len) {
  177. struct camd35 *c = &ts->camd35;
  178. int i;
  179. uint8_t *bdata = c->buf + 4; // Leave space for auth token
  180. camd35_connect(ts);
  181. camd35_init_auth(ts);
  182. memmove(bdata, c->buf, data_len); // Move data
  183. init_4b(c->auth_token, c->buf); // Put authentication token
  184. for (i = 0; i < data_len; i += 16) // Encrypt payload
  185. AES_encrypt(bdata + i, bdata + i, &c->aes_encrypt_key);
  186. return fdwrite(c->server_fd, (char *)c->buf, data_len + 4);
  187. }
  188. static void camd35_buf_init(struct camd35 *c, uint8_t *data, int data_len) {
  189. memset(c->buf, 0, CAMD35_HDR_LEN); // Reset header
  190. memset(c->buf + CAMD35_HDR_LEN, 0xff, CAMD35_BUF_LEN - CAMD35_HDR_LEN); // Reset data
  191. c->buf[1] = data_len; // Data length
  192. init_4b(crc32(0L, data, data_len), c->buf + 4); // Data CRC is at buf[4]
  193. memcpy(c->buf + CAMD35_HDR_LEN, data, data_len); // Copy data to buf
  194. }
  195. static int camd35_send_ecm(struct ts *ts, uint16_t ca_id, uint16_t service_id, uint16_t idx, uint8_t *data, uint8_t data_len) {
  196. struct camd35 *c = &ts->camd35;
  197. uint32_t provider_id = 0;
  198. int to_send = boundary(4, CAMD35_HDR_LEN + data_len);
  199. camd35_buf_init(c, data, (int)data_len);
  200. c->buf[0] = 0x00; // CMD ECM request
  201. init_2b(service_id , c->buf + 8);
  202. init_2b(ca_id , c->buf + 10);
  203. init_4b(provider_id, c->buf + 12);
  204. init_2b(idx , c->buf + 16);
  205. c->buf[18] = 0xff;
  206. c->buf[19] = 0xff;
  207. // OSCAM do not like it if ECM's are comming too fast
  208. // It thinks they are part of a single packet and ignores
  209. // the data at the end. The usleep() is a hack but works
  210. if (ts->packet_delay)
  211. usleep(ts->packet_delay);
  212. int ret = camd35_send_buf(ts, to_send);
  213. if (ret <= 0) {
  214. ts_LOGf("ERR | Error sending ecm packet, reconnecting to camd.\n");
  215. ts->is_cw_error = 1;
  216. camd35_reconnect(ts);
  217. return ret;
  218. }
  219. ret = camd35_recv_cw(ts);
  220. if (ret < 48) {
  221. ts->is_cw_error = 1;
  222. if (ts->key.ts && time(NULL) - ts->key.ts > KEY_VALID_TIME) {
  223. if (c->key->is_valid_cw)
  224. notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
  225. time(NULL) - ts->key.ts);
  226. c->key->is_valid_cw = 0;
  227. }
  228. return 0;
  229. }
  230. return ret;
  231. }
  232. static int camd35_send_emm(struct ts *ts, uint16_t ca_id, uint8_t *data, uint8_t data_len) {
  233. struct camd35 *c = &ts->camd35;
  234. uint32_t prov_id = 0;
  235. int to_send = boundary(4, CAMD35_HDR_LEN + data_len);
  236. camd35_buf_init(c, data, (int)data_len);
  237. c->buf[0] = 0x06; // CMD incomming EMM
  238. init_2b(ca_id , c->buf + 10);
  239. init_4b(prov_id, c->buf + 12);
  240. // OSCAM do not like it if EMM's are comming too fast
  241. // It thinks they are part of a single packet and ignores
  242. // the data at the end. The usleep() is a hack but works
  243. if (ts->packet_delay)
  244. usleep(ts->packet_delay);
  245. int ret = camd35_send_buf(ts, to_send);
  246. if (ret < 0) {
  247. c->emm_recv_errors++;
  248. if (c->emm_recv_errors >= EMM_RECV_ERRORS_LIMIT) {
  249. ts_LOGf("ERR | Error sending emm packet, reconnecting to camd.\n");
  250. camd35_reconnect(ts);
  251. c->emm_recv_errors = 0;
  252. }
  253. } else {
  254. c->emm_recv_errors = 0;
  255. }
  256. return ret;
  257. }
  258. static void camd_do_msg(struct camd_msg *msg) {
  259. if (msg->type == EMM_MSG) {
  260. msg->ts->emm_seen_count++;
  261. if (camd35_send_emm(msg->ts, msg->ca_id, msg->data, msg->data_len) > 0)
  262. msg->ts->emm_processed_count++;
  263. }
  264. if (msg->type == ECM_MSG) {
  265. msg->ts->ecm_seen_count++;
  266. if (camd35_send_ecm(msg->ts, msg->ca_id, msg->service_id, msg->idx, msg->data, msg->data_len) > 0)
  267. msg->ts->ecm_processed_count++;
  268. }
  269. camd_msg_free(&msg);
  270. }
  271. struct camd_msg *camd_msg_alloc_emm(uint16_t ca_id, uint8_t *data, uint8_t data_len) {
  272. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  273. c->type = EMM_MSG;
  274. c->ca_id = ca_id;
  275. c->data_len = data_len;
  276. memcpy(c->data, data, data_len);
  277. return c;
  278. }
  279. struct camd_msg *camd_msg_alloc_ecm(uint16_t ca_id, uint16_t service_id, uint16_t idx, uint8_t *data, uint8_t data_len) {
  280. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  281. c->type = ECM_MSG;
  282. c->idx = idx;
  283. c->ca_id = ca_id;
  284. c->service_id = service_id;
  285. c->data_len = data_len;
  286. memcpy(c->data, data, data_len);
  287. return c;
  288. }
  289. void camd_msg_free(struct camd_msg **pmsg) {
  290. struct camd_msg *m = *pmsg;
  291. if (m) {
  292. FREE(*pmsg);
  293. }
  294. }
  295. static void *camd_thread(void *in_ts) {
  296. struct ts *ts = in_ts;
  297. set_thread_name("tsdec-camd");
  298. while (1) {
  299. struct camd_msg *msg;
  300. void *req = queue_get(ts->camd35.req_queue); // Waits...
  301. if (!req || ts->camd_stop)
  302. break;
  303. msg = queue_get_nowait(ts->camd35.ecm_queue);
  304. if (!msg)
  305. msg = queue_get_nowait(ts->camd35.emm_queue);
  306. if (!msg)
  307. break;
  308. camd_do_msg(msg);
  309. }
  310. pthread_exit(0);
  311. }
  312. void camd_msg_process(struct ts *ts, struct camd_msg *msg) {
  313. msg->ts = ts;
  314. if (ts->camd35.thread) {
  315. if (msg->type == EMM_MSG)
  316. queue_add(ts->camd35.emm_queue, msg);
  317. if (msg->type == ECM_MSG)
  318. queue_add(ts->camd35.ecm_queue, msg);
  319. queue_add(ts->camd35.req_queue, msg);
  320. } else {
  321. camd_do_msg(msg);
  322. }
  323. }
  324. void camd_start(struct ts *ts) {
  325. camd35_connect(ts);
  326. // The input is not file, process messages using async thread
  327. if (!(ts->input.type == FILE_IO && ts->input.fd != 0)) {
  328. ts->camd35.req_queue = queue_new();
  329. ts->camd35.ecm_queue = queue_new();
  330. ts->camd35.emm_queue = queue_new();
  331. pthread_create(&ts->camd35.thread, NULL , &camd_thread, ts);
  332. }
  333. }
  334. void camd_stop(struct ts *ts) {
  335. ts->camd_stop = 1;
  336. if (ts->camd35.thread) {
  337. queue_wakeup(ts->camd35.req_queue);
  338. pthread_join(ts->camd35.thread, NULL);
  339. queue_free(&ts->camd35.req_queue);
  340. queue_free(&ts->camd35.ecm_queue);
  341. queue_free(&ts->camd35.emm_queue);
  342. ts->camd35.thread = 0;
  343. }
  344. camd35_disconnect(ts);
  345. }