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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 | Unxpected server response, skipping it (data[0] == 0x%02x /%s/)\n",
  115. data[0],
  116. data[0] == 0x08 ? "No card" : "Unknown");
  117. c->key->is_valid_cw = 0;
  118. memcpy(c->key->cw, invalid_cw, 16);
  119. return 0;
  120. }
  121. if (data_len < 48) {
  122. ts_LOGf("CW | data_len (%d) mismatch != 48\n", data_len);
  123. return 0;
  124. }
  125. if (data[1] < 0x10) {
  126. ts_LOGf("CW | CW len (%d) mismatch != 16\n", data[1]);
  127. return 0;
  128. }
  129. gettimeofday(&tv2, NULL);
  130. uint16_t ca_id = (data[10] << 8) | data[11];
  131. uint16_t idx = (data[16] << 8) | data[17];
  132. uint8_t *cw = data + 20;
  133. memcpy(c->key->cw, cw, 16);
  134. char cw_dump[16 * 6];
  135. ts_hex_dump_buf(cw_dump, 16 * 6, cw, 16, 0);
  136. int valid_cw = memcmp(c->key->cw, invalid_cw, 16) != 0;
  137. if (!c->key->is_valid_cw && valid_cw) {
  138. ts_LOGf("CW | OK: Valid CW was received.\n");
  139. }
  140. c->key->is_valid_cw = valid_cw;
  141. // At first ts_keyset is not initialized
  142. last_ts_keyset = c->key->ts_keyset;
  143. if (c->key->is_valid_cw) {
  144. gettimeofday(&c->key->ts_keyset, NULL);
  145. c->key->ts = c->key->ts_keyset.tv_sec;
  146. ts->cw_last_warn = c->key->ts;
  147. if (memcmp(c->key->cw, invalid_cw, 8) != 0) {
  148. dvbcsa_key_set (c->key->cw, c->key->csakey[0]);
  149. dvbcsa_bs_key_set(c->key->cw, c->key->bs_csakey[0]);
  150. }
  151. if (memcmp(c->key->cw + 8, invalid_cw, 8) != 0) {
  152. dvbcsa_key_set(c->key->cw + 8, c->key->csakey[1]);
  153. dvbcsa_bs_key_set(c->key->cw + 8, c->key->bs_csakey[1]);
  154. }
  155. }
  156. if (ts->ecm_cw_log) {
  157. ts_LOGf("CW | CAID: 0x%04x [ %5llu ms ] ( %6llu ms ) ------- IDX: 0x%04x Data: %s\n",
  158. ca_id, timeval_diff_msec(&tv1, &tv2),
  159. timeval_diff_msec(&last_ts_keyset, &tv2),
  160. idx, cw_dump );
  161. }
  162. return ret;
  163. }
  164. #undef ERR
  165. static int camd35_send_buf(struct ts *ts, int data_len) {
  166. struct camd35 *c = &ts->camd35;
  167. int i;
  168. uint8_t *bdata = c->buf + 4; // Leave space for auth token
  169. camd35_connect(ts);
  170. camd35_init_auth(ts);
  171. memmove(bdata, c->buf, data_len); // Move data
  172. init_4b(c->auth_token, c->buf); // Put authentication token
  173. for (i = 0; i < data_len; i += 16) // Encrypt payload
  174. AES_encrypt(bdata + i, bdata + i, &c->aes_encrypt_key);
  175. return fdwrite(c->server_fd, (char *)c->buf, data_len + 4);
  176. }
  177. static void camd35_buf_init(struct camd35 *c, uint8_t *data, int data_len) {
  178. memset(c->buf, 0, CAMD35_HDR_LEN); // Reset header
  179. memset(c->buf + CAMD35_HDR_LEN, 0xff, CAMD35_BUF_LEN - CAMD35_HDR_LEN); // Reset data
  180. c->buf[1] = data_len; // Data length
  181. init_4b(crc32(0L, data, data_len), c->buf + 4); // Data CRC is at buf[4]
  182. memcpy(c->buf + CAMD35_HDR_LEN, data, data_len); // Copy data to buf
  183. }
  184. 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) {
  185. struct camd35 *c = &ts->camd35;
  186. uint32_t provider_id = 0;
  187. int to_send = boundary(4, CAMD35_HDR_LEN + data_len);
  188. camd35_buf_init(c, data, (int)data_len);
  189. c->buf[0] = 0x00; // CMD ECM request
  190. init_2b(service_id , c->buf + 8);
  191. init_2b(ca_id , c->buf + 10);
  192. init_4b(provider_id, c->buf + 12);
  193. init_2b(idx , c->buf + 16);
  194. c->buf[18] = 0xff;
  195. c->buf[19] = 0xff;
  196. // OSCAM do not like it if ECM's are comming too fast
  197. // It thinks they are part of a single packet and ignores
  198. // the data at the end. The usleep() is a hack but works
  199. if (ts->packet_delay)
  200. usleep(ts->packet_delay);
  201. int ret = camd35_send_buf(ts, to_send);
  202. if (ret <= 0) {
  203. ts_LOGf("ECM | Error sending packet.\n");
  204. ts->is_cw_error = 1;
  205. camd35_reconnect(ts);
  206. return ret;
  207. }
  208. ret = camd35_recv_cw(ts);
  209. if (ret < 48) {
  210. ts->is_cw_error = 1;
  211. if (ts->key.ts && time(NULL) - ts->key.ts > KEY_VALID_TIME)
  212. c->key->is_valid_cw = 0;
  213. return 0;
  214. }
  215. return ret;
  216. }
  217. static int camd35_send_emm(struct ts *ts, uint16_t ca_id, uint8_t *data, uint8_t data_len) {
  218. struct camd35 *c = &ts->camd35;
  219. uint32_t prov_id = 0;
  220. int to_send = boundary(4, CAMD35_HDR_LEN + data_len);
  221. camd35_buf_init(c, data, (int)data_len);
  222. c->buf[0] = 0x06; // CMD incomming EMM
  223. init_2b(ca_id , c->buf + 10);
  224. init_4b(prov_id, c->buf + 12);
  225. // OSCAM do not like it if EMM's are comming too fast
  226. // It thinks they are part of a single packet and ignores
  227. // the data at the end. The usleep() is a hack but works
  228. if (ts->packet_delay)
  229. usleep(ts->packet_delay);
  230. int ret = camd35_send_buf(ts, to_send);
  231. if (ret <= 0) {
  232. ts_LOGf("EMM | Error sending packet.\n");
  233. camd35_reconnect(ts);
  234. }
  235. return ret;
  236. }
  237. static void camd_do_msg(struct camd_msg *msg) {
  238. if (msg->type == EMM_MSG) {
  239. msg->ts->emm_seen_count++;
  240. if (camd35_send_emm(msg->ts, msg->ca_id, msg->data, msg->data_len) > 0)
  241. msg->ts->emm_processed_count++;
  242. }
  243. if (msg->type == ECM_MSG) {
  244. msg->ts->ecm_seen_count++;
  245. if (camd35_send_ecm(msg->ts, msg->ca_id, msg->service_id, msg->idx, msg->data, msg->data_len) > 0)
  246. msg->ts->ecm_processed_count++;
  247. }
  248. camd_msg_free(&msg);
  249. }
  250. struct camd_msg *camd_msg_alloc_emm(uint16_t ca_id, uint8_t *data, uint8_t data_len) {
  251. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  252. c->type = EMM_MSG;
  253. c->ca_id = ca_id;
  254. c->data_len = data_len;
  255. memcpy(c->data, data, data_len);
  256. return c;
  257. }
  258. 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) {
  259. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  260. c->type = ECM_MSG;
  261. c->idx = idx;
  262. c->ca_id = ca_id;
  263. c->service_id = service_id;
  264. c->data_len = data_len;
  265. memcpy(c->data, data, data_len);
  266. return c;
  267. }
  268. void camd_msg_free(struct camd_msg **pmsg) {
  269. struct camd_msg *m = *pmsg;
  270. if (m) {
  271. FREE(*pmsg);
  272. }
  273. }
  274. static void *camd_thread(void *in_ts) {
  275. struct ts *ts = in_ts;
  276. while (1) {
  277. struct camd_msg *msg = queue_get(ts->camd35.queue); // Waits...
  278. if (!msg || ts->camd_stop)
  279. break;
  280. camd_do_msg(msg);
  281. }
  282. pthread_exit(0);
  283. }
  284. void camd_msg_process(struct ts *ts, struct camd_msg *msg) {
  285. msg->ts = ts;
  286. if (ts->camd35.thread) {
  287. queue_add(ts->camd35.queue, msg);
  288. } else {
  289. camd_do_msg(msg);
  290. }
  291. }
  292. void camd_start(struct ts *ts) {
  293. camd35_connect(ts);
  294. // The input is not file, process messages using async thread
  295. if (!(ts->input.type == FILE_IO && ts->input.fd != 0)) {
  296. ts->camd35.queue = queue_new();
  297. pthread_create(&ts->camd35.thread, NULL , &camd_thread, ts);
  298. }
  299. }
  300. void camd_stop(struct ts *ts) {
  301. ts->camd_stop = 1;
  302. if (ts->camd35.thread) {
  303. queue_wakeup(ts->camd35.queue);
  304. pthread_join(ts->camd35.thread, NULL);
  305. queue_free(&ts->camd35.queue);
  306. ts->camd35.thread = 0;
  307. }
  308. camd35_disconnect(ts);
  309. }