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

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