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.

fixups.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * CA system specific fixups
  3. * Copyright (C) 2010-2012 OSCAM Developers.
  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 <stdio.h>
  16. #include <stdarg.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include "libtsfuncs/tsfuncs.h"
  20. #include "fixups.h"
  21. static int DEBUG = 0;
  22. static uint8_t emm_global[1024];
  23. static int emm_global_len = 0;
  24. static void ts_LOGf_hd(uint8_t *buf, int len, const char *fmt, ...) {
  25. if (!DEBUG)
  26. return;
  27. char msg[1024];
  28. char msg2[1024];
  29. unsigned int i;
  30. va_list args;
  31. va_start(args, fmt);
  32. vsnprintf(msg, sizeof(msg)-1, fmt, args);
  33. va_end(args);
  34. msg[sizeof(msg)-2] = '\n';
  35. msg[sizeof(msg)-1] = '\0';
  36. ts_hex_dump_buf(msg2, sizeof(msg2), buf, len, 16);
  37. for (i = 0; i < strlen(msg); i++) {
  38. if (msg[i] == '\n')
  39. msg[i] = ' ';
  40. }
  41. ts_LOGf("XXX: %s, len: %d:\n%s\n", msg, len, msg2);
  42. }
  43. #define dbg_ts_LOGf_hd(...) \
  44. do { if (DEBUG) ts_LOGf_hd(__VA_ARGS__); } while (0)
  45. #define dbg_ts_LOGf(...) \
  46. do { if (DEBUG) ts_LOGf(__VA_ARGS__); } while (0)
  47. static void sort_nanos(unsigned char *dest, const unsigned char *src, int src_len) {
  48. int dst_pos = 0, marker = -1, src_pos, nano, nano_len;
  49. do {
  50. nano = 0x100;
  51. for (src_pos = 0; src_pos < src_len; ) {
  52. nano_len = src[src_pos + 1] + 2;
  53. if (src[src_pos] == marker) {
  54. if (dst_pos + nano_len > src_len) {
  55. // ERROR
  56. memset(dest, 0, src_len);
  57. return;
  58. }
  59. memcpy(dest + dst_pos, src + src_pos, nano_len);
  60. dst_pos += nano_len;
  61. } else if (src[src_pos] > marker && src[src_pos] < nano) {
  62. nano = src[src_pos];
  63. }
  64. src_pos += nano_len;
  65. }
  66. if (nano >= 0x100)
  67. break;
  68. marker = nano;
  69. } while (1);
  70. }
  71. int viaccess_reassemble_emm(uint8_t *buffer, unsigned int *len) {
  72. if (*len > 500)
  73. return 0;
  74. switch (buffer[0]) {
  75. case 0x8c:
  76. case 0x8d: { // emm-s part 1
  77. if (!memcmp(emm_global, buffer, *len))
  78. return 0;
  79. // copy first part of the emm-s
  80. memcpy(emm_global, buffer, *len);
  81. emm_global_len = *len;
  82. dbg_ts_LOGf_hd(buffer, *len, "viaccess global emm:\n");
  83. return 0;
  84. }
  85. case 0x8e: { // emm-s part 2
  86. if (!emm_global_len)
  87. return 0;
  88. int i, pos = 0;
  89. unsigned int k;
  90. // extract nanos from emm-gh and emm-s
  91. uint8_t emmbuf[1024];
  92. dbg_ts_LOGf("[viaccess] %s: start extracting nanos\n", __func__);
  93. // extract from emm-gh
  94. for (i = 3; i < emm_global_len; i += emm_global[i+1] + 2) {
  95. //copy nano (length determined by i+1)
  96. memcpy(emmbuf + pos, emm_global+i, emm_global[i+1] + 2);
  97. pos += emm_global[i+1] + 2;
  98. }
  99. if (buffer[2] == 0x2c) {
  100. // Add 9E 20 nano + first 32 bytes of emm content
  101. memcpy(emmbuf+pos, "\x9E\x20", 2);
  102. memcpy(emmbuf+pos+2, buffer+7, 32);
  103. pos += 34;
  104. //add F0 08 nano + 8 subsequent bytes of emm content
  105. memcpy(emmbuf+pos, "\xF0\x08", 2);
  106. memcpy(emmbuf+pos+2, buffer+39, 8);
  107. pos += 10;
  108. } else {
  109. // Extract from variable emm-s
  110. for (k = 7; k < (*len); k += buffer[k+1]+2) {
  111. // Copy nano (length determined by k+1)
  112. memcpy(emmbuf + pos, buffer + k, buffer[k + 1] + 2);
  113. pos += buffer[k + 1] + 2;
  114. }
  115. }
  116. dbg_ts_LOGf_hd(buffer, *len, "[viaccess] %s: %s emm-s\n", __func__, (buffer[2]==0x2c) ? "fixed" : "variable");
  117. sort_nanos(buffer + 7, emmbuf, pos);
  118. pos += 7;
  119. // Calculate emm length and set it on position 2
  120. buffer[2] = pos - 3;
  121. dbg_ts_LOGf_hd(emm_global, emm_global_len, "[viaccess] %s: emm-gh\n", __func__);
  122. dbg_ts_LOGf_hd(buffer , pos , "[viaccess] %s: assembled emm\n", __func__);
  123. *len = pos;
  124. break;
  125. }
  126. }
  127. return 1;
  128. }
  129. int cryptoworks_reassemble_emm(uint8_t *buffer, unsigned int *len) {
  130. if (*len > 500)
  131. return 0;
  132. // Cryptoworks
  133. // Cryptoworks EMM-S have to be assembled by the client from an EMM-SH with table
  134. // id 0x84 and a corresponding EMM-SB (body) with table id 0x86. A pseudo EMM-S
  135. // with table id 0x84 has to be build containing all nano commands from both the
  136. // original EMM-SH and EMM-SB in ascending order.
  137. //
  138. switch (buffer[0]) {
  139. case 0x84: { // emm-sh
  140. if (memcmp(emm_global, buffer, *len) == 0)
  141. return 0;
  142. memcpy(emm_global, buffer, *len);
  143. emm_global_len = *len;
  144. return 0;
  145. }
  146. case 0x86: { // emm-sb
  147. dbg_ts_LOGf_hd(buffer, *len, "[cryptoworks] shared emm (EMM-SB) /ORG/\n");
  148. if (!emm_global_len) {
  149. dbg_ts_LOGf("[cryptoworks] no 84 part yet.\n");
  150. return 0;
  151. }
  152. // We keep the first 12 bytes of the 0x84 emm (EMM-SH)
  153. // now we need to append the payload of the 0x86 emm (EMM-SB)
  154. // starting after the header (&buffer[5])
  155. // then the rest of the payload from EMM-SH
  156. // so we should have :
  157. // EMM-SH[0:12] + EMM-SB[5:len_EMM-SB] + EMM-SH[12:EMM-SH_len]
  158. // then sort the nano in ascending order
  159. // update the emm len (emmBuf[1:2])
  160. //
  161. int emm_len = *len - 5 + emm_global_len - 12;
  162. uint8_t tmp[emm_len];
  163. uint8_t assembled_EMM[emm_len + 12];
  164. memcpy(tmp, &buffer[5], *len - 5);
  165. memcpy(tmp + *len - 5, &emm_global[12], emm_global_len - 12);
  166. memcpy(assembled_EMM, emm_global, 12);
  167. sort_nanos(assembled_EMM + 12, tmp, emm_len);
  168. assembled_EMM[1] = ((emm_len + 9) >> 8) | 0x70;
  169. assembled_EMM[2] = (emm_len + 9) & 0xFF;
  170. // Copy back the assembled emm in the working buffer
  171. memcpy(buffer, assembled_EMM, emm_len + 12);
  172. *len = emm_len + 12;
  173. emm_global_len = 0;
  174. dbg_ts_LOGf_hd(buffer, emm_len + 12, "[cryptoworks] shared emm (assembled)\n");
  175. if (assembled_EMM[11] != emm_len) { // sanity check
  176. // error in emm assembly
  177. dbg_ts_LOGf("[cryptoworks] Error assembling Cryptoworks EMM-S %d != %d\n", assembled_EMM[11], emm_len);
  178. return 0;
  179. }
  180. break;
  181. }
  182. }
  183. return 1;
  184. }