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-cs378x.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * cs378x protocol
  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 (COPYING file) for more details.
  13. *
  14. */
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <openssl/aes.h>
  19. #include <openssl/md5.h>
  20. #include "libfuncs/libfuncs.h"
  21. #include "data.h"
  22. #include "util.h"
  23. #include "camd.h"
  24. static int cs378x_connect(struct camd *c) {
  25. if (c->server_fd < 0)
  26. c->server_fd = connect_client(SOCK_STREAM, c->hostname, c->service);
  27. return c->server_fd;
  28. }
  29. static void cs378x_disconnect(struct camd *c) {
  30. shutdown_fd(&c->server_fd);
  31. }
  32. static int cs378x_reconnect(struct camd *c) {
  33. cs378x_disconnect(c);
  34. return cs378x_connect(c);
  35. }
  36. static int cs378x_recv(struct camd *c, uint8_t *data, int *data_len) {
  37. int i;
  38. // Read AUTH token
  39. ssize_t r = fdread(c->server_fd, (char *)data, 4);
  40. if (r < 4)
  41. return -1;
  42. uint32_t auth_token = (((data[0] << 24) | (data[1] << 16) | (data[2]<<8) | data[3]) & 0xffffffffL);
  43. if (auth_token != c->cs378x.auth_token) {
  44. ts_LOGf("ERR | [%s] recv auth 0x%08x != camd_auth 0x%08x\n",
  45. c->ops.ident, auth_token, c->cs378x.auth_token);
  46. }
  47. *data_len = 256;
  48. for (i = 0; i < *data_len; i += 16) { // Read and decrypt payload
  49. fdread(c->server_fd, (char *)data + i, 16);
  50. AES_decrypt(data + i, data + i, &c->cs378x.aes_decrypt_key);
  51. if (i == 0)
  52. *data_len = boundary(4, data[1] + 20); // Initialize real data length
  53. }
  54. return *data_len;
  55. }
  56. static int cs378x_send_buf(struct camd *c, int data_len) {
  57. int i;
  58. unsigned char dump[16];
  59. if (cs378x_connect(c) < 0)
  60. return -1;
  61. // Prepare auth token (only once)
  62. if (!c->cs378x.auth_token) {
  63. c->cs378x.auth_token = crc32(0L, MD5((unsigned char *)c->user, strlen(c->user), dump), 16);
  64. MD5((unsigned char *)c->pass, strlen(c->pass), dump);
  65. AES_set_encrypt_key(dump, 128, &c->cs378x.aes_encrypt_key);
  66. AES_set_decrypt_key(dump, 128, &c->cs378x.aes_decrypt_key);
  67. }
  68. uint8_t *bdata = c->cs378x.buf + 4; // Leave space for auth token
  69. memmove(bdata, c->cs378x.buf, data_len); // Move data
  70. init_4b(c->cs378x.auth_token, c->cs378x.buf); // Put authentication token
  71. for (i = 0; i < data_len; i += 16) // Encrypt payload
  72. AES_encrypt(bdata + i, bdata + i, &c->cs378x.aes_encrypt_key);
  73. return fdwrite(c->server_fd, (char *)c->cs378x.buf, data_len + 4);
  74. }
  75. static void cs378x_buf_init(struct camd *c, uint8_t *data, int data_len) {
  76. memset(c->cs378x.buf, 0, CAMD35_HDR_LEN); // Reset header
  77. memset(c->cs378x.buf + CAMD35_HDR_LEN, 0xff, CAMD35_BUF_LEN - CAMD35_HDR_LEN); // Reset data
  78. c->cs378x.buf[1] = data_len; // Data length
  79. init_4b(crc32(0L, data, data_len), c->cs378x.buf + 4); // Data CRC is at buf[4]
  80. memcpy(c->cs378x.buf + CAMD35_HDR_LEN, data, data_len); // Copy data to buf
  81. }
  82. static int cs378x_do_ecm(struct camd *c, struct camd_msg *msg) {
  83. int to_send = boundary(4, CAMD35_HDR_LEN + msg->data_len);
  84. cs378x_buf_init(c, msg->data, (int)msg->data_len);
  85. c->cs378x.msg_id++;
  86. c->cs378x.buf[0] = 0x00; // CMD ECM request
  87. init_2b(msg->service_id , c->cs378x.buf + 8);
  88. init_2b(msg->ca_id , c->cs378x.buf + 10);
  89. init_4b(0 , c->cs378x.buf + 12); // Provider ID
  90. init_2b(c->cs378x.msg_id, c->cs378x.buf + 16);
  91. c->cs378x.buf[18] = 0xff;
  92. c->cs378x.buf[19] = 0xff;
  93. return cs378x_send_buf(c, to_send);
  94. }
  95. static int cs378x_do_emm(struct camd *c, struct camd_msg *msg) {
  96. int to_send = boundary(4, CAMD35_HDR_LEN + msg->data_len);
  97. cs378x_buf_init(c, msg->data, (int)msg->data_len);
  98. c->cs378x.buf[0] = 0x06; // CMD incomming EMM
  99. init_2b(msg->ca_id , c->cs378x.buf + 10);
  100. init_4b(0 , c->cs378x.buf + 12); // Provider ID
  101. return cs378x_send_buf(c, to_send);
  102. }
  103. static int cs378x_get_cw(struct camd *c, uint16_t *ca_id, uint16_t *idx, uint8_t *cw) {
  104. uint8_t *data = c->cs378x.buf;
  105. int data_len = 0;
  106. int ret = 0;
  107. READ:
  108. ret = cs378x_recv(c, data, &data_len);
  109. if (ret < 0) // Fatal error
  110. return -1;
  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 | [%s] Unexpected server response on code word request (ret data[0] == 0x%02x /%s/)\n",
  116. c->ops.ident,
  117. data[0],
  118. data[0] == 0x08 ? "No card" :
  119. data[0] == 0x44 ? "No code word found" : "Unknown err");
  120. return 0;
  121. }
  122. if (data_len < 48) {
  123. ts_LOGf("ERR | [%s] Code word packet len != 48 (%d)\n", c->ops.ident, data_len);
  124. return 0;
  125. }
  126. if (data[1] < 0x10) {
  127. ts_LOGf("ERR | [%s] Code word len != 16 (%d)\n", c->ops.ident, data[1]);
  128. return 0;
  129. }
  130. *ca_id = (data[10] << 8) | data[11];
  131. *idx = (data[16] << 8) | data[17];
  132. memcpy(cw, data + 20, 16);
  133. return 1;
  134. }
  135. void camd_proto_cs378x(struct camd_ops *ops) {
  136. ops->ident = "cs378x";
  137. ops->proto = CAMD_CS378X;
  138. ops->connect = cs378x_connect;
  139. ops->disconnect = cs378x_disconnect;
  140. ops->reconnect = cs378x_reconnect;
  141. ops->do_emm = cs378x_do_emm;
  142. ops->do_ecm = cs378x_do_ecm;
  143. ops->get_cw = cs378x_get_cw;
  144. }