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

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