PageRenderTime 3689ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/decrypt/decrypt.c

https://bitbucket.org/a84/gs_public/
C | 740 lines | 666 code | 54 blank | 20 comment | 171 complexity | 33ac1a858f6f023fb3e846a35a15e7a3 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Author: 2010 Andrey Dyldin <and@andmicro.com>
  3. * 2010 Santa77 <santa77@fibercom.sk>
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <unistd.h>
  10. #include "decrypt.h"
  11. #include "config.h"
  12. #include "../getstream.h"
  13. void getEmmPid(struct decrypt_s *decrypt, uint8_t *payload, uint16_t pid)
  14. {
  15. if(!payload || !decrypt->cam->loginOk)
  16. return;
  17. struct pid_s *pids = decrypt->stream->pids;
  18. struct ca_s *t = (struct ca_s *)pids[pid].data;
  19. if(t->caid == decrypt->cam->caId)
  20. pids[pid].type = MYEMM;
  21. else
  22. pids[pid].type = XEMM;
  23. }
  24. uint8_t viaccess_emm_check(struct decrypt_s *decrypt, uint8_t *payload)
  25. {
  26. switch(payload[0])
  27. {
  28. case 0x8E: // EMM-S (part2)
  29. return !memcmp(&payload[3], &decrypt->prov_id[4], 3);
  30. case 0x8C:
  31. case 0x8D: // EMM-GH (part1)
  32. return ((payload[3] == 0x90 || payload[3] == 0x40) && payload[4] == 0x03)
  33. && payload[5] == decrypt->prov_ident[0]
  34. && payload[6] == decrypt->prov_ident[1]
  35. && (payload[7] & 0xF0) == decrypt->prov_ident[2];
  36. case 0x88: // EMM-Unique
  37. return !memcmp(&payload[3], &decrypt->cam->ua[3], 5);
  38. case 0x8a: // EMM-Global
  39. case 0x8b: // EMM-Global
  40. return 1;
  41. default:
  42. break;
  43. }
  44. return 0; // false
  45. }
  46. uint8_t dre_emm_check(struct decrypt_s *decrypt, uint8_t *payload)
  47. {
  48. switch(payload[0])
  49. {
  50. case 0x87: // EMM-Unique
  51. {
  52. if(!memcmp(&payload[3], &decrypt->cam->ua[4], 4)
  53. && (payload[39] == 0x3d || payload[39] == 0x3b || payload[39] == 0x3a))
  54. {
  55. return 1; // true
  56. }
  57. break;
  58. }
  59. case 0x86: // EMM-Group
  60. case 0x88: // EMM-Group
  61. case 0x89: // EMM-Group
  62. {
  63. if(payload[3] == decrypt->cam->ua[4])
  64. return 1; // true
  65. break;
  66. }
  67. // case 0x89: // EMM-Shared
  68. default:
  69. break;
  70. }
  71. return 0; // false
  72. }
  73. uint8_t irdeto_emm_check(struct decrypt_s *decrypt, uint8_t *payload)
  74. {
  75. uint8_t len = payload[3] & 0x07; // 0
  76. int base = payload[3] >> 3; // 1C (28)
  77. if(base < 0x10)
  78. { // provider
  79. return (len <= 3 &&
  80. base == decrypt->prov_id[4] &&
  81. (!len || !memcmp(&payload[4],
  82. &decrypt->prov_id[5],
  83. len)) );
  84. }
  85. else
  86. { // card
  87. return (len <= 3 &&
  88. base == decrypt->cam->ua[4] &&
  89. (!len || !memcmp(&payload[4],
  90. &decrypt->cam->ua[5],
  91. len)) );
  92. }
  93. return 0;
  94. }
  95. uint8_t conax_emm_check(struct decrypt_s *decrypt, uint8_t *payload)
  96. {
  97. int ok = 0;
  98. ok = (!memcmp(&payload[6],&decrypt->prov_id[4],4));
  99. if (ok) { // provider, shared
  100. logwrite(G_LOG_INFO,
  101. "[Stream:%s] CONAX EMM: SHARED IS MINE. %02X%02X%02X%02X\n", decrypt->stream->name,
  102. payload[6],
  103. payload[7],
  104. payload[8],
  105. payload[9]);
  106. return 1;
  107. }else{ // unique, card
  108. if (!memcmp(&payload[6],&decrypt->cam->ua[4],4)) {
  109. logwrite(G_LOG_INFO,
  110. "[Stream:%s] CONAX EMM: UNIQUE IS MINE. %02X%02X%02X%02X\n", decrypt->stream->name,
  111. payload[6],
  112. payload[7],
  113. payload[8],
  114. payload[9]);
  115. return 1;
  116. }
  117. } // there are not global at conax? (no info found)
  118. return 0; // false
  119. }
  120. uint8_t cryptoworks_emm_check(struct decrypt_s *decrypt, uint8_t *payload)
  121. {
  122. switch(payload[0])
  123. {
  124. case 0x82: // CRYPTOWORKS EMM: UNIQUE
  125. {
  126. if(payload[3]==0xA9 && payload[4]==0xFF && payload[13]==0x80 && payload[14]==0x05) { // EMM is OK
  127. if (!memcmp(&payload[5],&decrypt->cam->ua[3],5)){
  128. logwrite(G_LOG_DEBUG,"CRYPTOWORKS EMM: UNIQUE MINE");
  129. return 1;
  130. }
  131. }
  132. break;
  133. }
  134. case 0x84: // CRYPTOWORKS EMM: SHARED
  135. {
  136. if(payload[3]==0xA9 && payload[4]==0xFF && payload[12]==0x80 && payload[13]==0x04) {
  137. if (!memcmp(&payload[5],&decrypt->cam->ua[3],4)){
  138. logwrite(G_LOG_DEBUG,"[Stream:%s] CRYPTOWORKS EMM: SHARED IS MINE. %02X%02X%02X%02X%02X", decrypt->stream->name,
  139. payload[5],
  140. payload[6],
  141. payload[7],
  142. payload[8],
  143. payload[9]);
  144. return 1;
  145. }
  146. }
  147. break;
  148. }
  149. case 0x86: // CRYPTOWORKS EMM: GLOBAL
  150. if(payload[3]==0xA9 && payload[4]==0xFF && payload[5]==0x83 && payload[6]==0x01 && payload[8]==0x85) {
  151. logwrite(G_LOG_DEBUG,"[Stream:%s] CRYPTOWORKS EMM: GLOBAL 0x86 v 0x85", decrypt->stream->name);
  152. return 1;
  153. }
  154. if(payload[3]==0xA9 && payload[4]==0xFF && payload[5]==0x83 && payload[6]==0x01 && payload[8]==0x84) {
  155. logwrite(G_LOG_DEBUG,"[Stream:%s] CRYPTOWORKS EMM: GLOBAL 0x86 v 0x84 not sent. not known.", decrypt->stream->name);
  156. return 0;
  157. }
  158. case 0x88:
  159. case 0x89:
  160. {
  161. if(payload[3]==0xA9 && payload[4]==0xFF && payload[8]==0x83 && payload[9]==0x01) {
  162. logwrite(G_LOG_DEBUG,"[Stream:%s] CRYPTOWORKS EMM: GLOBAL 0x%02X", decrypt->stream->name, payload[0]);
  163. return 1;
  164. }
  165. break;
  166. }
  167. case 0x8F:
  168. {
  169. logwrite(G_LOG_DEBUG,"[Stream:%s] CRYPTOWORKS EMM: GLOBAL 0x8F via camd3 %02X. Should it TODO?!", decrypt->stream->name, payload[0]);
  170. break;
  171. }
  172. default:
  173. logwrite(G_LOG_DEBUG,"[Stream:%s] CRYPTOWORKS EMM: UNKNOWN %02X", decrypt->stream->name, payload[0]);
  174. break;
  175. }
  176. return 0; // false
  177. }
  178. uint8_t videoguard_emm_check(struct decrypt_s *decrypt, uint8_t *payload) {
  179. unsigned char emm_type = (payload[3] & 0xC0) >> 6;
  180. switch(emm_type) {
  181. case 0: // global
  182. return 1; // true
  183. case 1: // unique
  184. case 2: // shared
  185. {
  186. unsigned char serial_count = ((payload[3] >> 4) & 3) + 1;
  187. unsigned char serial_len = (payload[3] & 0x80) ? 3 : 4;
  188. if(payload[0] == 0) return 1; // true
  189. int i;
  190. for(i = 0; i < serial_count; i++) {
  191. if(!memcmp(&payload[i * 4 + 4], &decrypt->cam->ua[4], serial_len)) {
  192. return 1; // true
  193. }
  194. }
  195. break;
  196. }
  197. default:
  198. break;
  199. }
  200. return 0; // false
  201. }
  202. uint8_t nagra_emm_check(struct decrypt_s *decrypt, uint8_t *payload) {
  203. switch(payload[0]) {
  204. case 0x83: {
  205. if(payload[7] == 0x10) { // shared
  206. return ( (payload[5] == decrypt->cam->ua[4])
  207. && (payload[4] == decrypt->cam->ua[5])
  208. && (payload[3] == decrypt->cam->ua[6])
  209. );
  210. }
  211. else { // unique
  212. return ( (payload[5] == decrypt->cam->ua[4])
  213. && (payload[4] == decrypt->cam->ua[5])
  214. && (payload[3] == decrypt->cam->ua[6])
  215. && (payload[6] == decrypt->cam->ua[7])
  216. );
  217. }
  218. break;
  219. }
  220. case 0x82: // global
  221. default:
  222. return 1; // true
  223. }
  224. return 0; // false
  225. }
  226. void emm_callback(void *arg, cam_packet_t *packet, uint8_t *result)
  227. {
  228. struct decrypt_s *decrypt = (struct decrypt_s *)arg;
  229. if(result != NULL)
  230. decrypt->stream->mon_err_code |= MERR_DECRYPT_EMM;
  231. }
  232. uint8_t isNull(uint8_t *data, size_t len) {
  233. while(len-- > 0) if(data[len]) return 0; // false
  234. return 1; // true
  235. }
  236. // from sasc :)
  237. static void SortNanos(unsigned char *dest, const unsigned char *src, int len)
  238. {
  239. int w=0, c=-1;
  240. while(1) {
  241. int n=0x100;
  242. for(int j=0; j<len;) {
  243. int l=src[j+1]+2;
  244. if(src[j]==c) {
  245. if(w+l>len) {
  246. printf("sortnanos: sanity check failed. Exceeding memory area. Probably corrupted nanos!");
  247. memset(dest,0,len); // zero out everything
  248. return;
  249. }
  250. memcpy(&dest[w],&src[j],l);
  251. w+=l;
  252. }
  253. else if(src[j]>c && src[j]<n)
  254. n=src[j];
  255. j+=l;
  256. }
  257. if(n==0x100) break;
  258. c=n;
  259. }
  260. }
  261. void processEMM(struct decrypt_s *decrypt, uint8_t *payload)
  262. {
  263. if(payload[0] < 0x82 || payload[0] > 0x8F) return;
  264. if(!payload || !decrypt->cam->loginOk || !decrypt->prov_id)
  265. return;
  266. uint8_t caid8 = decrypt->cam->caId >> 8;
  267. switch(caid8)
  268. {
  269. case 0x05: // Viaccess
  270. if(!viaccess_emm_check(decrypt, payload))
  271. return;
  272. if(isNull(&payload[3], 16)) return;
  273. break;
  274. case 0x4A: // DRE
  275. if(!dre_emm_check(decrypt, payload))
  276. return;
  277. break;
  278. case 0x06: // Irdeto
  279. if(!irdeto_emm_check(decrypt, payload))
  280. return;
  281. break;
  282. case 0x0B: // Conax
  283. if(!conax_emm_check(decrypt, payload))
  284. return;
  285. break;
  286. case 0x0D: // CryptoWorks
  287. if(!cryptoworks_emm_check(decrypt, payload))
  288. return;
  289. break;
  290. case 0x09: // VideoGuard
  291. if(!videoguard_emm_check(decrypt, payload))
  292. return;
  293. break;
  294. case 0x18: // nagra
  295. if(!nagra_emm_check(decrypt, payload))
  296. return;
  297. break;
  298. default: // not handled CAS
  299. /* It's spammy on streams where are more providers and more CAS used */
  300. // printf("[Stream:%s] unknown CAS in %s: %02X\n",
  301. // decrypt->stream->name, __func__, t->caid >> 8);
  302. return;
  303. break;
  304. }
  305. uint16_t emm_len = SCT_LEN(payload);
  306. if (emm_len>CWS_NETMSGSIZE-7){
  307. return; //Seems to be broken EMM.
  308. }
  309. uint8_t buffer[CWS_NETMSGSIZE];
  310. if(caid8 == 0x05) {
  311. switch(payload[0]) {
  312. case 0x8C:
  313. case 0x8D: {
  314. // EMM-GH
  315. if (decrypt->viaccess.sharedEmm && !memcmp(decrypt->viaccess.sharedEmm, payload, emm_len)){
  316. //new packet is equal to previous
  317. return;
  318. }
  319. if(payload[0] != decrypt->viaccess.sharedToggle) {
  320. if(decrypt->viaccess.sharedEmm) free(decrypt->viaccess.sharedEmm);
  321. decrypt->viaccess.sharedLen = emm_len;
  322. decrypt->viaccess.sharedEmm = (uint8_t *)malloc(emm_len);
  323. memcpy(decrypt->viaccess.sharedEmm, payload, emm_len);
  324. decrypt->viaccess.sharedToggle = payload[0];
  325. }
  326. return;
  327. }
  328. case 0x8E: {
  329. // EMM-S
  330. if(!decrypt->viaccess.sharedEmm) break;
  331. uint8_t tmp[CWS_NETMSGSIZE];
  332. int len = emm_len - 7; // emm header size
  333. if((payload[6] & 0x02) == 0x00) {
  334. uint8_t addrlen = len - 8;
  335. len = 0;
  336. tmp[len++] = 0x9E; // adf
  337. tmp[len++] = addrlen;
  338. memcpy(&tmp[len], &payload[7], addrlen); // &payload[7] - viaccess payload
  339. len += addrlen;
  340. tmp[len++] = 0xF0;
  341. tmp[len++] = 0x08;
  342. memcpy(&tmp[len], &payload[7+addrlen], 8);
  343. len += 8;
  344. }
  345. else {
  346. memcpy(tmp, &payload[7], len);
  347. }
  348. int len_s = 3; // shared emm header size
  349. int l = decrypt->viaccess.sharedLen - len_s;
  350. memcpy(&tmp[len], &decrypt->viaccess.sharedEmm[len_s], l);
  351. len += l;
  352. memcpy(buffer, payload, 7);
  353. SortNanos(&buffer[7], tmp, len);
  354. buffer[2] = len + 4;
  355. emm_len = SCT_LEN(buffer);
  356. payload = buffer;
  357. break;
  358. }
  359. }
  360. }
  361. if (decrypt->cam->log_emm){
  362. monitor_send_msg(decrypt->stream->config->monitor,
  363. "[CAM: %d] Add EMM from stream \"%s\", type:%02X, length: %d(0x%02X)"
  364. ,decrypt->cam->id,decrypt->stream->name,payload[0],emm_len,emm_len
  365. );
  366. }
  367. cam_packet_t packet;
  368. packet.type = PACKET_EMM;
  369. memcpy(packet.data, payload, emm_len);
  370. packet.len = emm_len;
  371. packet.prg_id = 0;
  372. packet.response_time = 0;
  373. packet.callback = emm_callback;
  374. packet.callback_arg = decrypt;
  375. cam_add_packet(decrypt->cam, &packet);
  376. }
  377. void _processEMM_callback(void *arg, mpeg2_psi_t *psi) {
  378. processEMM((struct decrypt_s *)arg, psi->payload);
  379. }
  380. void getEcmPid(struct decrypt_s *decrypt, uint8_t *payload, uint16_t pid)
  381. {
  382. if(!payload || !decrypt->cam->loginOk)
  383. return;
  384. struct pid_s *pids = decrypt->stream->pids;
  385. struct ca_s *t = (struct ca_s *)pids[pid].data;
  386. int i;
  387. switch(t->caid >> 8)
  388. {
  389. case 0x05: // Viaccess
  390. {
  391. if(decrypt->prov_id != NULL)
  392. {
  393. pids[pid].type = XECM;
  394. return;
  395. }
  396. payload += 4; // skip first bytes of ecm
  397. if(payload[0] == 0xD2) payload += (payload[1] + 2); // skip tps
  398. for(i = 0; i < decrypt->cam->num_of_provs; i++)
  399. {
  400. if(decrypt->cam->provs_ident[i][2] == (payload[4] & 0xF0) &&
  401. decrypt->cam->provs_ident[i][1] == payload[3] &&
  402. decrypt->cam->provs_ident[i][0] == payload[2])
  403. {
  404. pids[pid].type = MYECM;
  405. decrypt->prov_id = decrypt->cam->provs_id[i];
  406. decrypt->prov_ident = decrypt->cam->provs_ident[i];
  407. return;
  408. }
  409. }
  410. break;
  411. }
  412. case 0x4A: // DRE
  413. if(t->caid == decrypt->cam->caId)
  414. {
  415. pids[pid].type = MYECM;
  416. decrypt->prov_id = decrypt->cam->provs_id[0];
  417. decrypt->prov_ident = decrypt->cam->provs_ident[0];
  418. return;
  419. }
  420. break;
  421. case 0x06: // Irdeto
  422. if(t->caid == decrypt->cam->caId)
  423. {
  424. pids[pid].type = MYECM;
  425. decrypt->prov_id = decrypt->cam->provs_id[0];
  426. decrypt->prov_ident = decrypt->cam->provs_ident[0];
  427. return;
  428. }
  429. break;
  430. case 0x0B: // Conax
  431. if(t->caid == decrypt->cam->caId)
  432. {
  433. pids[pid].type = MYECM;
  434. decrypt->prov_id = decrypt->cam->provs_id[0];
  435. decrypt->prov_ident = decrypt->cam->provs_ident[0];
  436. return;
  437. }
  438. break;
  439. case 0x0D: // CryptoWorks
  440. if(t->caid == decrypt->cam->caId)
  441. {
  442. logwrite(G_LOG_DEBUG,"[Stream:%s] CryptoWorks was chosen\n", decrypt->stream->name);
  443. pids[pid].type = MYECM;
  444. decrypt->prov_id = decrypt->cam->provs_id[0];
  445. decrypt->prov_ident = decrypt->cam->provs_ident[0];
  446. return;
  447. }
  448. break;
  449. case 0x09: // VideoGuard
  450. if(t->caid == decrypt->cam->caId)
  451. {
  452. pids[pid].type = MYECM;
  453. decrypt->prov_id = decrypt->cam->provs_id[0];
  454. decrypt->prov_ident = decrypt->cam->provs_ident[0];
  455. return;
  456. }
  457. break;
  458. case 0x18: // nagra
  459. if(t->caid == decrypt->cam->caId)
  460. {
  461. pids[pid].type = MYECM;
  462. decrypt->prov_id = decrypt->cam->provs_id[0];
  463. decrypt->prov_ident = decrypt->cam->provs_ident[0];
  464. return;
  465. }
  466. default:
  467. /* It's spammy on streams where are more providers and more CAS used */
  468. // printf("[Stream:%s] unknown CAS in %s: %02X\n",
  469. // decrypt->stream->name, __func__, t->caid >> 8);
  470. break;
  471. }
  472. }
  473. void ecm_callback(void *arg, cam_packet_t *packet, uint8_t *keys)
  474. {
  475. struct decrypt_s *decrypt = (struct decrypt_s *)arg;
  476. if ( (decrypt->cam->log_ecm_mintime > 0 && packet->response_time <= (uint16_t)decrypt->cam->log_ecm_mintime)
  477. || (decrypt->cam->log_ecm_maxtime > 0 && packet->response_time >= (uint16_t)decrypt->cam->log_ecm_maxtime)) {
  478. monitor_send_msg(decrypt->stream->config->monitor,
  479. "[Stream:%s] ecm response time: %dms", decrypt->stream->name,
  480. packet->response_time);
  481. }
  482. if(keys != NULL)
  483. {
  484. struct timeval nowTime;
  485. uint32_t lastCWTimeDiff ;
  486. gettimeofday(&nowTime, NULL);
  487. if (decrypt->lastCWTime.tv_usec>0){
  488. lastCWTimeDiff = ((nowTime.tv_sec - decrypt->lastCWTime.tv_sec) * 1000) +
  489. ((nowTime.tv_usec - decrypt->lastCWTime.tv_usec) / 1000);
  490. if ( !memcmp(&decrypt->lastCW[0],&keys[0],8)
  491. || !memcmp(&decrypt->lastCW[8],&keys[8],8)
  492. //One of the keys is equal to previous - seems good CW
  493. ){
  494. } else {
  495. monitor_send_msg(decrypt->stream->config->monitor
  496. , "[Stream:%s] WARNING: different CW,"
  497. " PREV: %02X%02X%02X%02X%02X%02X%02X%02X:%02X%02X%02X%02X%02X%02X%02X%02X"
  498. " NOW: %02X%02X%02X%02X%02X%02X%02X%02X:%02X%02X%02X%02X%02X%02X%02X%02X (%dms)"
  499. , decrypt->stream->name,
  500. decrypt->lastCW[0],decrypt->lastCW[1],decrypt->lastCW[2],decrypt->lastCW[3],
  501. decrypt->lastCW[4],decrypt->lastCW[5],decrypt->lastCW[6],decrypt->lastCW[7],
  502. decrypt->lastCW[8],decrypt->lastCW[9],decrypt->lastCW[10],decrypt->lastCW[11],
  503. decrypt->lastCW[12],decrypt->lastCW[13],decrypt->lastCW[14],decrypt->lastCW[15],
  504. keys[0],keys[1],keys[2],keys[3], keys[4],keys[5],keys[6],keys[7],
  505. keys[8],keys[9],keys[10],keys[11], keys[12],keys[13],keys[14],keys[15],
  506. lastCWTimeDiff
  507. );
  508. }
  509. }
  510. memcpy(decrypt->lastCW,keys,16);
  511. gettimeofday(&decrypt->lastCWTime, NULL);
  512. decrypt->lastParity = packet->data[0];
  513. if(decrypt->ecm_number == 0xFF
  514. && (decrypt->cam->caId >> 8) == 0x06) { // irdeto
  515. decrypt->ecm_number = packet->data[4];
  516. }
  517. memcpy(decrypt->ckeys, keys, 16);
  518. }
  519. else
  520. decrypt->stream->mon_err_code |= MERR_DECRYPT_ECM;
  521. }
  522. void processECM(struct decrypt_s *decrypt, uint8_t *payload)
  523. {
  524. if(!payload || payload[0] == decrypt->lastParity)
  525. return; // keys not change
  526. decrypt->lastParity = payload[0];
  527. cam_packet_t packet;
  528. packet.type = PACKET_ECM;
  529. packet.prg_id = decrypt->prg_id;
  530. packet.response_time = 0;
  531. packet.callback = ecm_callback;
  532. packet.callback_arg = decrypt;
  533. switch(payload[0])
  534. {
  535. case 0x80: // ecm odd
  536. case 0x81: // ecm even
  537. {
  538. packet.len = SCT_LEN(payload);
  539. memcpy(packet.data, payload, packet.len);
  540. cam_add_packet(decrypt->cam, &packet);
  541. break;
  542. }
  543. // case MSG_KEEPALIVE:
  544. default:
  545. break;
  546. }
  547. }
  548. void _processECM_callback(void *arg, mpeg2_psi_t *psi) {
  549. struct decrypt_s *decrypt = (struct decrypt_s *)arg;
  550. int caid8 = decrypt->cam->caId & 0xFF00;
  551. if(caid8 == 0x0600) {
  552. if(decrypt->stream->ecm_group > 0) {
  553. if(decrypt->stream->ecm_group != psi->payload[7]) return;
  554. }
  555. else {
  556. if(decrypt->ecm_number == 0xFF) {
  557. decrypt->lastParity = 0;
  558. }
  559. else {
  560. if(decrypt->ecm_number != psi->payload[4]) return;
  561. }
  562. }
  563. }
  564. processECM(decrypt, psi->payload);
  565. }
  566. uint8_t *get_payload(uint8_t *ts)
  567. {
  568. uint8_t afield = 0;
  569. switch(ts[3] & 0x30)
  570. {
  571. case 0x30: // ts[4] - adaptation length; 1 - length of ts[4]
  572. afield = ts[4] + 1;
  573. break;
  574. case 0x10: // no adaptation field
  575. break;
  576. case 0x20: // adaptation field, no payload
  577. default: // no payload
  578. return NULL;
  579. }
  580. uint8_t pusi = (ts[1] & 0x40)?(ts[4 + afield] + 1):0;
  581. uint8_t i_hdr = 4 + pusi + afield;
  582. return &ts[i_hdr];
  583. }
  584. void parse_tspacket(struct decrypt_s *decrypt, void *data, int len)
  585. {
  586. if(!decrypt->cam->loginOk)
  587. return;
  588. uint8_t *ts = (uint8_t *)data;
  589. struct pid_s *pids = decrypt->stream->pids;
  590. uint16_t pid = ((ts[1] & 0x1F) << 8) | ts[2];
  591. switch(pids[pid].type)
  592. {
  593. case PAT:
  594. case CAT:
  595. case PMT:
  596. case NIT:
  597. case XEMM:
  598. case XECM:
  599. return;
  600. case EMM:
  601. {
  602. if(decrypt->cam->disable_emm > 0) {
  603. pids[pid].type = XEMM;
  604. return;
  605. }
  606. getEmmPid(decrypt, get_payload(ts), pid);
  607. if(pids[pid].type == MYEMM) {
  608. monitor_send_msg(decrypt->stream->config->monitor
  609. , "[Stream:%s] Select EMM: pid=%d"
  610. , decrypt->stream->name, pid);
  611. }
  612. return;
  613. }
  614. case MYEMM:
  615. {
  616. mpegts_mux_psi(&decrypt->psi_emm, ts, _processEMM_callback, decrypt);
  617. return;
  618. }
  619. case ECM:
  620. {
  621. getEcmPid(decrypt, get_payload(ts), pid);
  622. if(pids[pid].type == MYECM) {
  623. monitor_send_msg(decrypt->stream->config->monitor
  624. , "[Stream:%s] Select ECM: pid=%d"
  625. , decrypt->stream->name, pid);
  626. }
  627. return;
  628. }
  629. case MYECM:
  630. {
  631. mpegts_mux_psi(&decrypt->psi_ecm, ts, _processECM_callback, decrypt);
  632. return;
  633. }
  634. default:
  635. break;
  636. }
  637. }
  638. int decrypt_init(struct decrypt_s *decrypt)
  639. {
  640. decrypt->keys = get_key_struct();
  641. memset(decrypt->lastCW,0,sizeof(decrypt->lastCW));
  642. switch(decrypt->type)
  643. {
  644. case DECRYPT_BISS:
  645. {
  646. if(decrypt->ckeys[3] == 0x00) {
  647. decrypt->ckeys[3] = (decrypt->ckeys[0]
  648. + decrypt->ckeys[1]
  649. + decrypt->ckeys[2]) & 0xFF;
  650. }
  651. if(decrypt->ckeys[7] == 0x00) {
  652. decrypt->ckeys[7] = (decrypt->ckeys[4]
  653. + decrypt->ckeys[5]
  654. + decrypt->ckeys[6]) & 0xFF;
  655. }
  656. #ifdef DEBUG
  657. printf("Set BISS key: %02X%02X%02X%02X%02X%02X%02X%02X\n",
  658. decrypt->ckeys[0], decrypt->ckeys[1],
  659. decrypt->ckeys[2], decrypt->ckeys[3],
  660. decrypt->ckeys[4], decrypt->ckeys[5],
  661. decrypt->ckeys[6], decrypt->ckeys[7]);
  662. #endif
  663. set_control_words(decrypt->keys, decrypt->ckeys, decrypt->ckeys);
  664. break;
  665. }
  666. case DECRYPT_NEWCAMD:
  667. {
  668. // bug#66
  669. // while(decrypt->cam->loginOk <= 0)
  670. // sleep(1);
  671. switch(decrypt->cam->caId >> 8) {
  672. case 0x06: { // irdeto
  673. decrypt->ecm_number = 0xFF;
  674. break;
  675. }
  676. default:
  677. break;
  678. }
  679. memset(decrypt->ckeys, 0, sizeof(decrypt->ckeys));
  680. break;
  681. }
  682. default:
  683. return 0;
  684. }
  685. return 1; // true
  686. }