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

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