source: trunk/reader-videoguard2.c@ 4149

Last change on this file since 4149 was 4141, checked in by dingo35, 13 years ago

all: simplify debug system, add D_DVBAPI = -d128, eliminate cs_ptyp which complicates stuff unnecc

File size: 24.8 KB
Line 
1#include "globals.h"
2#include "reader-common.h"
3#include "reader-videoguard-common.h"
4
5// Redefine logging funtion to include reader name
6#define cs_log(x...) cs_log("[videoguard2-reader] "x)
7#define cs_ri_log(x,y...) cs_ri_log(x,"[videoguard2-reader] "y)
8#ifdef WITH_DEBUG
9 #define cs_debug_mask(x,y...) cs_debug_mask(x, "[videoguard2-reader] "y)
10#endif
11
12static void dimeno_PostProcess_Decrypt(struct s_reader * reader, unsigned char *rxbuff, unsigned char *cw)
13{
14 unsigned char tag,len,len2;
15 bool valid_0x55=FALSE;
16 unsigned char *body;
17 unsigned char buffer[0x10];
18 int a=0x13;
19 len2=rxbuff[4];
20 while(a<len2+5-9) // +5 for 5 ins bytes, -9 (body=8 len=1) to prevent memcpy(buffer+8,body,8) from reading past rxbuff
21 {
22 tag=rxbuff[a];
23 len=rxbuff[a+1];
24 body=rxbuff+a+2;
25 switch(tag)
26 {
27 case 0x55:{
28 if(body[0]==0x84){ //Tag 0x56 has valid data...
29 valid_0x55=TRUE;
30 }
31 }break;
32 case 0x56:{
33 memcpy(buffer+8,body,8);
34 }break;
35 }
36 a+=len+2;
37 }
38 if(valid_0x55){
39 memcpy(buffer,rxbuff+5,8);
40 AES_decrypt(buffer,buffer,&(reader->astrokey));
41 memcpy(cw+0,buffer,8); // copy calculated CW in right place
42 }
43}
44
45static void do_post_dw_hash(unsigned char *cw, unsigned char *ecm_header_data)
46{
47 int i, ecmi, ecm_header_count;
48 unsigned char buffer[0x80];
49 unsigned char md5_digest[0x10];
50 static const unsigned short Hash3[] = {0x0123,0x4567,0x89AB,0xCDEF,0xF861,0xCB52};
51 static const unsigned char Hash4[] = {0x0B,0x04,0x07,0x08,0x05,0x09,0x0B,0x0A,0x07,0x02,0x0A,0x05,0x04,0x08,0x0D,0x0F};
52 static const unsigned short NdTabB001[0x15][0x20] = {
53 {0xEAF1, 0x0237, 0x29D0, 0xBAD2, 0xE9D3, 0x8BAE, 0x2D6D, 0xCD1B,
54 0x538D, 0xDE6B, 0xA634, 0xF81A, 0x18B5, 0x5087, 0x14EA, 0x672E,
55 0xF0FC, 0x055E, 0x62E5, 0xB78F, 0x5D09, 0x0003, 0xE4E8, 0x2DCE,
56 0x6BE0, 0xAC4E, 0xF485, 0x6967, 0xF28C, 0x97A0, 0x01EF, 0x0100},
57 {0xC539, 0xF5B9, 0x9099, 0x013A, 0xD4B9, 0x6AB5, 0xEA67, 0x7EB4,
58 0x6C30, 0x4BF0, 0xB810, 0xB0B5, 0xB76D, 0xA751, 0x1AE7, 0x14CA,
59 0x4F4F, 0x1586, 0x2608, 0x10B1, 0xE7E1, 0x48BE, 0x7DDD, 0x5ECB,
60 0xCFBF, 0x323B, 0x8B31, 0xB131, 0x0F1A, 0x664B, 0x0140, 0x0100},
61 {0x3C7D, 0xBDC4, 0xFEC7, 0x26A6, 0xB0A0, 0x6E55, 0xF710, 0xF9BF,
62 0x0023, 0xE81F, 0x41CA, 0xBE32, 0xB461, 0xE92D, 0xF1AF, 0x409F,
63 0xFC85, 0xFE5B, 0x7FCE, 0x17F5, 0x01AB, 0x4A46, 0xEB05, 0xA251,
64 0xDC6F, 0xF0C0, 0x10F0, 0x1D51, 0xEFAA, 0xE9BF, 0x0100, 0x0100},
65 {0x1819, 0x0CAA, 0x9067, 0x607A, 0x7576, 0x1CBC, 0xE51D, 0xBF77,
66 0x7EC6, 0x839E, 0xB695, 0xF096, 0xDC10, 0xCB69, 0x4654, 0x8E68,
67 0xD62D, 0x4F1A, 0x4227, 0x92AC, 0x9064, 0x6BD1, 0x1E75, 0x2747,
68 0x00DA, 0xA6A6, 0x6CF1, 0xD151, 0xBE56, 0x3E33, 0x0128, 0x0100},
69 {0x4091, 0x09ED, 0xD494, 0x6054, 0x1869, 0x71D5, 0xB572, 0x7BF1,
70 0xE925, 0xEE2D, 0xEEDE, 0xA13C, 0x6613, 0x9BAB, 0x122D, 0x7AE4,
71 0x5268, 0xE6C9, 0x50CB, 0x79A1, 0xF212, 0xA062, 0x6B48, 0x70B3,
72 0xF6B0, 0x06D5, 0xF8AB, 0xECF5, 0x6255, 0xEDD8, 0x79D2, 0x290A},
73 {0xD3CF, 0x014E, 0xACB3, 0x8F6B, 0x0F2C, 0xA5D8, 0xE8E0, 0x863D,
74 0x80D5, 0x5705, 0x658A, 0x8BC2, 0xEE46, 0xD3AE, 0x0199, 0x0100,
75 0x4A35, 0xABE4, 0xF976, 0x935A, 0xA8A5, 0xBAE9, 0x24D0, 0x71AA,
76 0xB3FE, 0x095E, 0xAB06, 0x4CD5, 0x2F0D, 0x1ACB, 0x59F3, 0x4C50},
77 {0xFD27, 0x0F8E, 0x191A, 0xEEE7, 0x2F49, 0x3A05, 0x3267, 0x4F88,
78 0x38AE, 0xFCE9, 0x9476, 0x18C6, 0xF961, 0x4EF0, 0x39D0, 0x42E6,
79 0xB747, 0xE625, 0xB68E, 0x5100, 0xF92A, 0x86FE, 0xE79B, 0xEE91,
80 0x21D5, 0x4C3C, 0x683D, 0x5AD1, 0x1B49, 0xF407, 0x0194, 0x0100},
81 {0x4BF9, 0xDC0D, 0x9478, 0x5174, 0xCB4A, 0x8A89, 0x4D6A, 0xFED8,
82 0xF123, 0xA8CD, 0xEEE7, 0xA6D1, 0xB763, 0xF5E2, 0xE085, 0x01EF,
83 0xE466, 0x9FA3, 0x2F68, 0x2190, 0x423F, 0x287F, 0x7F3F, 0x09F6,
84 0x2111, 0xA963, 0xD0BB, 0x674A, 0xBA72, 0x45F9, 0xF186, 0xB8F5},
85 {0x0010, 0xD1B9, 0xB164, 0x9E87, 0x1F49, 0x6950, 0x2DBF, 0x38D3,
86 0x2EB0, 0x3E8E, 0x91E6, 0xF688, 0x7E41, 0x566E, 0x01B0, 0x0100,
87 0x24A1, 0x73D8, 0xA0C3, 0xF71B, 0xA0A5, 0x2A06, 0xBA46, 0xFEC3,
88 0xDD4C, 0x52CC, 0xF9BC, 0x3B7E, 0x3812, 0x0666, 0xB74B, 0x40F8},
89 {0x28F2, 0x7C81, 0xFC92, 0x6FBD, 0x53D6, 0x72A3, 0xBBDF, 0xB6FC,
90 0x9CE5, 0x2331, 0xD4F6, 0xC5BB, 0xE8BB, 0x6676, 0x02D9, 0x2F0E,
91 0xD009, 0xD136, 0xCD09, 0x7551, 0x1826, 0x9D9B, 0x63EA, 0xFC63,
92 0x68CD, 0x3672, 0xCB95, 0xD28E, 0xF1CD, 0x20CA, 0x014C, 0x0100},
93 {0xE539, 0x55B7, 0x989D, 0x21C4, 0x463A, 0xE68F, 0xF8B5, 0xE5C5,
94 0x662B, 0x35BF, 0x3C50, 0x0131, 0xF4BF, 0x38B2, 0x41BC, 0xB829,
95 0x02B7, 0x6B8F, 0xA25C, 0xAFD2, 0xD84A, 0x2243, 0x53EB, 0xC6C9,
96 0x2E14, 0x181F, 0x8F96, 0xDF0E, 0x0D4C, 0x30F6, 0xFFE1, 0x9DDA},
97 {0x30B6, 0x777E, 0xDA3D, 0xAF77, 0x205E, 0xC90B, 0x856B, 0xB451,
98 0x3BCC, 0x76C2, 0x8ACF, 0xDCB1, 0xA5E5, 0xDD64, 0x0197, 0x0100,
99 0xE751, 0xB661, 0x0404, 0xDB4A, 0xE9DD, 0xA400, 0xAF26, 0x3F5E,
100 0x904B, 0xA924, 0x09E0, 0xE72B, 0x825B, 0x2C50, 0x6FD0, 0x0D52},
101 {0x2730, 0xC2BA, 0x9E44, 0x5815, 0xFC47, 0xB21D, 0x67B8, 0xF8B9,
102 0x047D, 0xB0AF, 0x9F14, 0x741B, 0x4668, 0xBE54, 0xDE16, 0xDB14,
103 0x7CB7, 0xF2B8, 0x0683, 0x762C, 0x09A0, 0x9507, 0x7F92, 0x022C,
104 0xBA6A, 0x7D52, 0x0AF4, 0x1BC3, 0xB46A, 0xC4FD, 0x01C2, 0x0100},
105 {0x7611, 0x66F3, 0xEE87, 0xEDD3, 0xC559, 0xEFD4, 0xDC59, 0xF86B,
106 0x6D1C, 0x1C85, 0x9BB1, 0x3373, 0x763F, 0x4EBE, 0x1BF3, 0x99B5,
107 0xD721, 0x978F, 0xCF5C, 0xAC51, 0x0984, 0x7462, 0x8F0C, 0x2817,
108 0x4AD9, 0xFD41, 0x6678, 0x7C85, 0xD330, 0xC9F8, 0x1D9A, 0xC622},
109 {0x5AE4, 0xE16A, 0x60F6, 0xFD45, 0x668C, 0x29D6, 0x0285, 0x6B92,
110 0x92C2, 0x21DE, 0x45E0, 0xEF3D, 0x8B0D, 0x02CD, 0x0198, 0x0100,
111 0x9E6D, 0x4D38, 0xDEF9, 0xE6F2, 0xF72E, 0xB313, 0x14F2, 0x390A,
112 0x2D67, 0xC71E, 0xCB69, 0x7F66, 0xD3CF, 0x7F8A, 0x81D9, 0x9DDE},
113 {0x85E3, 0x8F29, 0x36EB, 0xC968, 0x3696, 0x59F6, 0x7832, 0xA78B,
114 0xA1D8, 0xF5CF, 0xAB64, 0x646D, 0x7A2A, 0xBAF8, 0xAA87, 0x41C7,
115 0x5120, 0xDE78, 0x738D, 0xDC1A, 0x268D, 0x5DF8, 0xED69, 0x1C8A,
116 0xBC85, 0x3DCD, 0xAE30, 0x0F8D, 0xEC89, 0x3ABD, 0x0166, 0x0100},
117 {0xB8BD, 0x643B, 0x748E, 0xBD63, 0xEC6F, 0xE23A, 0x9493, 0xDD76,
118 0x0A62, 0x774F, 0xCD68, 0xA67A, 0x9A23, 0xC8A8, 0xBDE5, 0x9D1B,
119 0x2B86, 0x8B36, 0x5428, 0x1DFB, 0xCD1D, 0x0713, 0x29C2, 0x8E8E,
120 0x5207, 0xA13F, 0x6005, 0x4F5E, 0x52E0, 0xE7C8, 0x6D1C, 0x3E34},
121 {0x581D, 0x2BFA, 0x5E1D, 0xA891, 0x1069, 0x1DA4, 0x39A0, 0xBE45,
122 0x5B9A, 0x7333, 0x6F3E, 0x8637, 0xA550, 0xC9E9, 0x5C6C, 0x42BA,
123 0xA712, 0xC3EA, 0x3808, 0x0910, 0xAA4D, 0x5B25, 0xABCD, 0xE680,
124 0x96AD, 0x2CEC, 0x8EBB, 0xA47D, 0x1690, 0xE8FB, 0x01C8, 0x0100},
125 {0x73B9, 0x82BC, 0x9EBC, 0xB130, 0x0DA5, 0x8617, 0x9F7B, 0x9766,
126 0x205D, 0x752D, 0xB05C, 0x2A17, 0xA75C, 0x18EF, 0x8339, 0xFD34,
127 0x8DA2, 0x7970, 0xD0B4, 0x70F1, 0x3765, 0x7380, 0x7CAF, 0x570E,
128 0x6440, 0xBC44, 0x0743, 0x2D02, 0x0419, 0xA240, 0x2113, 0x1AD4},
129 {0x1EB5, 0xBBFF, 0x39B1, 0x3209, 0x705F, 0x15F4, 0xD7AD, 0x340B,
130 0xC2A6, 0x25CA, 0xF412, 0x9570, 0x0F4F, 0xE4D5, 0x1614, 0xE464,
131 0x911A, 0x0F0E, 0x07DA, 0xA929, 0x2379, 0xD988, 0x0AA6, 0x3B57,
132 0xBF63, 0x71FB, 0x72D5, 0x26CE, 0xB0AF, 0xCF45, 0x011B, 0x0100},
133 {0x9999, 0x98FE, 0xA108, 0x6588, 0xF90B, 0x4554, 0xFF38, 0x4642,
134 0x8F5F, 0x6CC3, 0x4E8E, 0xFF7E, 0x64C2, 0x50CA, 0x0E7F, 0xAD7D,
135 0x6AAB, 0x33C1, 0xE1F4, 0x6165, 0x7894, 0x83B9, 0x0A0C, 0x38AF,
136 0x5803, 0x18C0, 0xFA36, 0x592C, 0x4548, 0xABB8, 0x1527, 0xAEE9}
137 };
138
139
140 //ecm_header_data = 01 03 b0 01 01
141 if (!cw_is_valid(cw,0)) //if cw is all zero, keep it that way
142 {
143 return;
144 }
145 ecm_header_count = ecm_header_data[0];
146 for (i = 0, ecmi = 1; i < ecm_header_count; i++)
147 {
148 if (ecm_header_data[ecmi + 1] != 0xb0)
149 {
150 ecmi += ecm_header_data[ecmi] + 1;
151 }
152 else
153 {
154 switch (ecm_header_data[ecmi + 2])
155 { //b0 01
156 case 1:
157 {
158 unsigned short hk[8], i, j, m = 0;
159 for (i = 0; i < 6; i++)
160 hk[2 + i] = Hash3[i];
161 for (i = 0; i < 2; i++)
162 {
163 for (j = 0; j < 0x48; j += 2)
164 {
165 if (i)
166 {
167 hk[0] = ((hk[3] & hk[5]) | ((~hk[5]) & hk[4]));
168 }
169 else
170 {
171 hk[0] = ((hk[3] & hk[4]) | ((~hk[3]) & hk[5]));
172 }
173 if (j < 8)
174 {
175 hk[0] = (hk[0] + ((cw[j + 1] << 8) | cw[j]));
176 }
177 if (j == 8)
178 {
179 hk[0] = (hk[0] + 0x80);
180 }
181 hk[0] = (hk[0] + hk[2] + (0xFF & NdTabB001[ecm_header_data[ecmi + 3]][m >> 1] >> ((m & 1) << 3)));
182 hk[1] = hk[2];
183 hk[2] = hk[3];
184 hk[3] = hk[4];
185 hk[4] = hk[5];
186 hk[5] = hk[6];
187 hk[6] = hk[7];
188 hk[7] = hk[2] + (((hk[0] << Hash4[m & 0xF]) | (hk[0] >> (0x10 - Hash4[m & 0xF]))));
189 m = (m + 1) & 0x3F;
190 }
191 }
192 for (i = 0; i < 6; i++)
193 {
194 hk[2 + i] += Hash3[i];
195 }
196 for (i = 0; i < 7; i++)
197 {
198 cw[i] = hk[2 + (i >> 1)] >> ((i & 1) << 3);
199 }
200 cw[3] = (cw[0] + cw[1] + cw[2]) & 0xFF;
201 cw[7] = (cw[4] + cw[5] + cw[6]) & 0xFF;
202 cs_ddump_mask(D_READER, cw, 8, "[videoguard2-reader] Postprocessed Case 1 DW:");
203 break;
204 }
205 case 3:
206 {
207 memset(buffer, 0, sizeof(buffer));
208 memcpy(buffer, cw, 8);
209 memcpy(buffer + 8, &ecm_header_data[ecmi + 3], ecm_header_data[ecmi] - 2);
210 MD5(buffer, 8 + ecm_header_data[ecmi] - 2, md5_digest);
211 memcpy(cw, md5_digest, 8);
212 cs_ddump_mask(D_READER, cw, 8, "[videoguard2-reader] Postprocessed Case 3 DW:");
213 break;
214 }
215 case 2:
216 {
217 /* Method 2 left out */
218 //memcpy(DW_OUTPUT, DW_INPUT, 8);
219 break;
220 }
221 }
222 }
223 }
224}
225
226
227static void vg2_read_tiers(struct s_reader * reader)
228{
229 def_resp;
230 int l;
231
232 /* ins2a is not needed and causes an error on some cards eg Sky Italy 09CD
233 check if ins2a is in command table before running it
234 */
235 static const unsigned char ins2a[5] = { 0xD0,0x2a,0x00,0x00,0x00 };
236 if(cmd_exists(reader,ins2a)) {
237 l=do_cmd(reader,ins2a,NULL,NULL,cta_res);
238 if(l<0 || !status_ok(cta_res+l)){
239 cs_log("classD0 ins2a: failed");
240 return;
241 }
242 }
243
244 static const unsigned char ins76007f[5] = { 0xD0,0x76,0x00,0x7f,0x02 };
245 if(!write_cmd_vg(ins76007f,NULL) || !status_ok(cta_res+2)){
246 cs_log("classD0 ins76007f: failed");
247 return;
248 }
249 int num=cta_res[1];
250
251 int i;
252 unsigned char ins76[5] = { 0xD0,0x76,0x00,0x00,0x00 };
253
254 // some cards start real tiers info in middle of tier info
255 // and have blank tiers between old tiers and real tiers eg 09AC
256 int starttier;
257 bool stopemptytier = TRUE;
258 if((starttier = reader->card_tierstart) == -1){
259 stopemptytier = FALSE;
260 starttier = 0;
261 }
262 // check to see if specified start tier is blank and if blank, start at 0 and ignore blank tiers
263 ins76[2]=starttier;
264 l=do_cmd(reader,ins76,NULL,NULL,cta_res);
265 if(l<0 || !status_ok(cta_res+l)) return;
266 if(cta_res[2]==0 && cta_res[3]==0 ){
267 stopemptytier = FALSE;
268 starttier = 0;
269 }
270 for(i=starttier; i<num; i++) {
271 ins76[2]=i;
272 l=do_cmd(reader,ins76,NULL,NULL,cta_res);
273 if(l<0 || !status_ok(cta_res+l)) return;
274 if(cta_res[2]==0 && cta_res[3]==0 && stopemptytier) return;
275 if(cta_res[2]!=0 || cta_res[3]!=0) {
276 int y,m,d,H,M,S;
277 rev_date_calc(&cta_res[4],&y,&m,&d,&H,&M,&S,reader->card_baseyear);
278 unsigned short tier_id = (cta_res[2] << 8) | cta_res[3];
279 char *tier_name = get_tiername(tier_id, reader->caid[0]);
280 if(!stopemptytier){
281 cs_debug_mask(D_READER, "tier: %04x, tier-number: 0x%02x",tier_id,i);
282 }
283 cs_ri_log(reader, "tier: %04x, expiry date: %04d/%02d/%02d-%02d:%02d:%02d %s",tier_id,y,m,d,H,M,S,tier_name);
284 }
285 }
286}
287
288static int videoguard2_card_init(struct s_reader * reader, ATR newatr)
289{
290 get_hist;
291 if ((hist_size < 7) || (hist[1] != 0xB0) || (hist[4] != 0xFF) || (hist[5] != 0x4A) || (hist[6] != 0x50)){
292 cs_debug_mask(D_READER, "failed history check");
293 return ERROR;
294 }
295 cs_debug_mask(D_READER, "passed history check");
296
297 get_atr;
298 def_resp;
299
300 /* set information on the card stored in reader-videoguard-common.c */
301 set_known_card_info(reader,atr,&atr_size);
302
303 if((reader->ndsversion != NDS2) &&
304 (((reader->card_system_version != NDS2) && (reader->card_system_version != NDSUNKNOWN)) ||
305 (reader->ndsversion != NDSAUTO))) {
306 /* known ATR and not NDS2
307 or known NDS2 ATR and forced to another NDS version */
308 return ERROR;
309 }
310
311 cs_debug_mask(D_READER, "type: %s, baseyear: %i", reader->card_desc, reader->card_baseyear);
312 if(reader->ndsversion == NDS2){
313 cs_debug_mask(D_READER, "forced to NDS2");
314 }
315
316 //a non videoguard2/NDS2 card will fail on read_cmd_len(ins7401)
317 //this way unknown videoguard2/NDS2 cards will also pass this check
318
319 unsigned char ins7401[5] = { 0xD0,0x74,0x01,0x00,0x00 };
320 int l;
321 ins7401[3]=0x80; // from newcs log
322 ins7401[4]=0x01;
323 if((l=read_cmd_len(reader,ins7401))<0){ //not a videoguard2/NDS card or communication error
324 return ERROR;
325 }
326 ins7401[3]=0x00;
327 ins7401[4]=l;
328 if(!write_cmd_vg(ins7401,NULL) || !status_ok(cta_res+l)) {
329 cs_log("classD0 ins7401: failed - cmd list not read");
330 return ERROR;
331 }
332
333 memorize_cmd_table (reader,cta_res,l);
334
335 unsigned char buff[256];
336
337 static const unsigned char ins7416[5] = { 0xD0,0x74,0x16,0x00,0x00 };
338 if(do_cmd(reader,ins7416,NULL,NULL,cta_res)<0) {
339 cs_log("classD0 ins7416: failed");
340 return ERROR;
341 }
342
343 unsigned char ins36[5] = { 0xD0,0x36,0x00,0x00,0x00 };
344 static const unsigned char ins5e[5] = { 0xD0,0x5E,0x00,0x0C,0x02 };
345 unsigned char boxID [4];
346
347 if (reader->boxid > 0) {
348 /* the boxid is specified in the config */
349 int i;
350 for (i=0; i < 4; i++) {
351 boxID[i] = (reader->boxid >> (8 * (3 - i))) % 0x100;
352 }
353 } else {
354 /* we can try to get the boxid from the card */
355 int boxidOK=0;
356 if((ins36[4]=read_cmd_len(reader,ins36))==0 && cmd_exists(reader,ins5e)) {
357 if(!write_cmd_vg(ins5e,NULL) || !status_ok(cta_res+2)){
358 cs_log("classD0 ins5e: failed");
359 } else {
360 ins36[3] = cta_res[0];
361 ins36[4] = cta_res[1];
362 }
363 }
364 l=ins36[4];
365 if(!write_cmd_vg(ins36,NULL) || !status_ok(cta_res+l)){
366 cs_log("classD0 ins36: failed");
367 return ERROR;
368 }
369 memcpy(buff,ins36,5);
370 memcpy(buff+5,cta_res,l);
371 memcpy(buff+5+l,cta_res+l,2);
372 if(l<13)
373 cs_log("classD0 ins36: answer too short");
374 else if (buff[7] > 0x0F)
375 cs_log("classD0 ins36: encrypted - can't parse");
376 else {
377 /* skipping the initial fixed fields: cmdecho (4) + length (1) + encr/rev++ (4) */
378 int i=9;
379 int gotUA=0;
380 while (i<l) {
381 if (!gotUA && buff[i]<0xF0) { /* then we guess that the next 4 bytes is the UA */
382 gotUA=1;
383 i+=4;
384 } else switch (buff[i]) { /* object length vary depending on type */
385 case 0x00: /* padding */
386 i+=1;
387 break;
388 case 0xEF: /* card status */
389 i+=3;
390 break;
391 case 0xD1:
392 i+=4;
393 break;
394 case 0xDF: /* next server contact */
395 i+=5;
396 break;
397 case 0xF3: /* boxID */
398 memcpy(boxID,buff+i+1,sizeof(boxID));
399 boxidOK=1;
400 i+=5;
401 break;
402 case 0xF6:
403 i+=6;
404 break;
405 case 0x01: /* date & time */
406 i+=7;
407 break;
408 case 0xFA:
409 i+=9;
410 break;
411 case 0x5E:
412 case 0x67: /* signature */
413 case 0xDE:
414 case 0xE2:
415 case 0xE9: /* tier dates */
416 case 0xF8: /* Old PPV Event Record */
417 case 0xFD:
418 i+=buff[i+1]+2; /* skip length + 2 bytes (type and length) */
419 break;
420 default: /* default to assume a length byte */
421 cs_log("classD0 ins36: returned unknown type=0x%02X - parsing may fail", buff[i]);
422 i+=buff[i+1]+2;
423 }
424 }
425 }
426
427 if(!boxidOK) {
428 cs_log ("no boxID available");
429 return ERROR;
430 }
431 }
432
433 static const unsigned char ins4C[5] = { 0xD0,0x4C,0x00,0x00,0x09 };
434 unsigned char payload4C[9] = { 0,0,0,0, 3,0,0,0,4 };
435 memcpy(payload4C,boxID,4);
436 if(!write_cmd_vg(ins4C,payload4C) || !status_ok(cta_res+l)) {
437 cs_log("classD0 ins4C: failed - sending boxid failed");
438 return ERROR;
439 }
440
441 //short int SWIRDstatus = cta_res[1];
442 static const unsigned char ins58[5] = { 0xD0,0x58,0x00,0x00,0x00 };
443 l=do_cmd(reader,ins58,NULL,NULL,cta_res);
444 if(l<0) {
445 cs_log("classD0 ins58: failed");
446 return ERROR;
447 }
448 memset(reader->hexserial, 0, 8);
449 memcpy(reader->hexserial+2, cta_res+3, 4);
450 memcpy(reader->sa, cta_res+3, 3);
451 reader->caid[0] = cta_res[24]*0x100+cta_res[25];
452
453 /* we have one provider, 0x0000 */
454 reader->nprov = 1;
455 memset(reader->prid, 0x00, sizeof(reader->prid));
456
457 /*
458 cs_log ("INS58 : Fuse byte=0x%02X, IRDStatus=0x%02X", cta_res[2],SWIRDstatus);
459 if (SWIRDstatus==4) {
460 // If swMarriage=4, not married then exchange for BC Key
461 cs_log ("Card not married, exchange for BC Keys");
462 */
463
464 cCamCryptVG_SetSeed(reader);
465
466 static const unsigned char insB4[5] = { 0xD0,0xB4,0x00,0x00,0x40 };
467 unsigned char tbuff[64];
468 cCamCryptVG_GetCamKey(reader,tbuff);
469 l=do_cmd(reader,insB4,tbuff,NULL,cta_res);
470 if(l<0 || !status_ok(cta_res)) {
471 cs_log("classD0 insB4: failed");
472 return ERROR;
473 }
474
475 static const unsigned char insBC[5] = { 0xD0,0xBC,0x00,0x00,0x00 };
476 l=do_cmd(reader,insBC,NULL,NULL,cta_res);
477 if(l<0) {
478 cs_log("classD0 insBC: failed");
479 return ERROR;
480 }
481
482 // Class D1/D3 instructions only work after this point
483
484 static const unsigned char insBE[5] = { 0xD3,0xBE,0x00,0x00,0x00 };
485 l=do_cmd(reader,insBE,NULL,NULL,cta_res);
486 if(l<0) {
487 cs_log("classD3 insBE: failed");
488 return ERROR;
489 }
490
491 static const unsigned char ins58a[5] = { 0xD1,0x58,0x00,0x00,0x00 };
492 l=do_cmd(reader,ins58a,NULL,NULL,cta_res);
493 if(l<0) {
494 cs_log("classD1 ins58: failed");
495 return ERROR;
496 }
497
498 static const unsigned char ins4Ca[5] = { 0xD1,0x4C,0x00,0x00,0x00 };
499 l=do_cmd(reader,ins4Ca,payload4C,NULL,cta_res);
500 if(l<0 || !status_ok(cta_res)) {
501 cs_log("classD1 ins4Ca: failed");
502 return ERROR;
503 }
504
505
506 /* get parental lock settings */
507 static const unsigned char ins74e[5] = {0xD0,0x74,0x0E,0x00,0x00};
508 if(cmd_exists(reader,ins74e)) {
509 l=do_cmd(reader,ins74e,NULL,NULL,cta_res);
510 if (l<0 || !status_ok(cta_res+l)) {
511 cs_log("classD0 ins74e: failed to get parental lock settings");
512 } else {
513 cs_log("parental lock setting: %s",cs_hexdump(1, cta_res+2, l-2));
514 }
515 }
516
517 /* disable parental lock */
518 static const uchar ins2e[5] = {0xD0, 0x2E, 0x00, 0x00, 0x04};
519 static const uchar payload2e[4] = {0xFF, 0xFF, 0xFF, 0xFF};
520 if(cfg->ulparent) {
521 if(cmd_exists(reader,ins74e) && write_cmd_vg(ins2e,payload2e) && status_ok(cta_res+l)) {
522 cs_log("parental lock disabled");
523 }else{
524 cs_log("cannot disable parental lock");
525 }
526 if(cmd_exists(reader,ins74e)) {
527 l=do_cmd(reader,ins74e,NULL,NULL,cta_res);
528 if (l<0 || !status_ok(cta_res+l)) {
529 cs_log("classD0 ins74e: failed to get parental lock settings");
530 } else {
531 cs_log("parental lock setting after disabling: %s",cs_hexdump(1, cta_res+2, l-2));
532 }
533 }
534 }
535
536 // fix for 09ac cards
537 unsigned char dimeno_magic[0x10]={0xF9,0xFB,0xCD,0x5A,0x76,0xB5,0xC4,0x5C,0xC8,0x2E,0x1D,0xE1,0xCC,0x5B,0x6B,0x02};
538 int a;
539 for(a=0; a<4; a++)
540 dimeno_magic[a]=dimeno_magic[a]^boxID[a];
541 AES_set_decrypt_key(dimeno_magic,128,&(reader->astrokey));
542
543 cs_ri_log(reader, "type: %s, caid: %04X",
544 reader->card_desc,
545 reader->caid[0]);
546 cs_ri_log(reader, "serial: %02X%02X%02X%02X, BoxID: %02X%02X%02X%02X, baseyear: %i",
547 reader->hexserial[2],reader->hexserial[3],reader->hexserial[4],reader->hexserial[5],
548 boxID[0],boxID[1],boxID[2],boxID[3],
549 reader->card_baseyear);
550 cs_log("ready for requests");
551
552 return OK;
553}
554
555static int videoguard2_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
556{
557 unsigned char cta_res[CTA_RES_LEN];
558 unsigned char ins40[5] = { 0xD1,0x40,0x00,0x80,0xFF };
559 static const unsigned char ins54[5] = { 0xD3,0x54,0x00,0x00,0x00};
560 int posECMpart2=er->ecm[6]+7;
561 int lenECMpart2=er->ecm[posECMpart2]+1;
562 unsigned char tbuff[264], rbuff[264];
563 tbuff[0]=0;
564
565 memset(er->cw+0,0,16); //set cw to 0 so client will know it is invalid unless it is overwritten with a valid cw
566 memcpy(tbuff+1,er->ecm+posECMpart2+1,lenECMpart2-1);
567
568/*
569 //log parental lock byte
570 int j;
571 for (j = posECMpart2+1; j < lenECMpart2+posECMpart2+1-4; j++){
572 if (er->ecm[j] == 0x02 && er->ecm[j+3] == 0x02) {
573 cs_log("channel parental lock mask: %02X%02X, channel parental lock byte: %02X",er->ecm[j+1],er->ecm[j+2],er->ecm[j+4]);
574 break;
575 }
576 }
577
578 //log tiers
579 int k;
580 for (k = posECMpart2+1; k < lenECMpart2+posECMpart2+1-4; k++){
581 if (er->ecm[k] == 0x03 && er->ecm[k+3] == 0x80) {
582 unsigned short vtier_id = (er->ecm[k+1] << 8) | er->ecm[k+2];
583 char *vtier_name = get_tiername(vtier_id, reader->caid[0]);
584 cs_log("valid tier: %04x %s",vtier_id,vtier_name);
585 }
586 }
587*/
588
589 ins40[4]=lenECMpart2;
590 int l;
591
592 l = do_cmd(reader,ins40,tbuff,NULL,cta_res);
593 if(l<0 || !status_ok(cta_res)) {
594 cs_log ("classD0 ins40: (%d) status not ok %02x %02x",l,cta_res[0],cta_res[1]);
595 return ERROR;
596 } else {
597 l = do_cmd(reader,ins54,NULL,rbuff,cta_res);
598 if(l<0 || !status_ok(cta_res+l)) {
599 cs_log("classD3 ins54: (%d) status not ok %02x %02x",l,cta_res[0],cta_res[1]);
600 return ERROR;
601 } else {
602
603 // Log decrypted INS54
604 // cs_dump (rbuff, 5, "[videoguard2-reader] Decrypted INS54:");
605 // cs_dump (rbuff + 5, rbuff[4], "");
606
607 if (!cw_is_valid(rbuff+5,0)){ //sky cards report 90 00 = ok but send cw = 00 when channel not subscribed
608 cs_log("classD3 ins54: status 90 00 = ok but cw=00 -> channel not subscribed " );
609 return ERROR;
610 }
611
612 // copy cw1 in place
613 memcpy(er->cw+0,rbuff+5,8);
614
615 // process cw2
616 int ind;
617 for(ind=15; ind<l+5-10; ind++) { // +5 for 5 ins bytes, -10 to prevent memcpy ind+3,8 from reading past
618 // rxbuffer we start searching at 15 because start at 13 goes wrong
619 // with 090F 090b and 096a
620 if(rbuff[ind]==0x25) {
621 memcpy(er->cw+8,rbuff+ind+3,8); //tested on viasat 093E, sky uk 0963, sky it 919
622 //don't care whether cw is 0 or not
623 break;
624 }
625 }
626
627 // fix for 09ac cards
628 dimeno_PostProcess_Decrypt(reader, rbuff, er->cw);
629
630 //test for postprocessing marker
631 int posB0 = -1;
632 int i;
633 for (i = 6; i < posECMpart2; i++){
634 if (er->ecm[i-3] == 0x80 && er->ecm[i] == 0xB0 && ((er->ecm[i+1] == 0x01) ||(er->ecm[i+1] == 0x02)||(er->ecm[i+1] == 0x03) ) ) {
635 posB0 = i;
636 break;
637 }
638 }
639 if (posB0 != -1) {
640 do_post_dw_hash( er->cw+0, &er->ecm[posB0-2]);
641 do_post_dw_hash( er->cw+8, &er->ecm[posB0-2]);
642 }
643
644 if(er->ecm[0]&1) {
645 unsigned char tmpcw[8];
646 memcpy(tmpcw,er->cw+8,8);
647 memcpy(er->cw+8,er->cw+0,8);
648 memcpy(er->cw+0,tmpcw,8);
649 }
650
651 return OK;
652 }
653 }
654}
655
656static int videoguard2_do_emm(struct s_reader * reader, EMM_PACKET *ep)
657{
658 unsigned char cta_res[CTA_RES_LEN];
659 unsigned char ins42[5] = { 0xD1,0x42,0x00,0x00,0xFF };
660 int rc=ERROR;
661
662 const unsigned char *payload = payload_addr(ep->type, ep->emm, reader->hexserial);
663 while (payload) {
664 ins42[4]=*payload;
665 int l = do_cmd(reader,ins42,payload+1,NULL,cta_res);
666 if(l>0 && status_ok(cta_res)) {
667 rc=OK;
668 }
669
670 cs_debug_mask(D_EMM, "EMM request return code : %02X%02X", cta_res[0], cta_res[1]);
671 //cs_dump(ep->emm, 64, "EMM:");
672 if (status_ok (cta_res) && (cta_res[1] & 0x01)) {
673 vg2_read_tiers(reader);
674 }
675
676 if (num_addr(ep->emm) == 1 && (int)(&payload[1] - &ep->emm[0]) + *payload + 1 < ep->l) {
677 payload += *payload + 1;
678 if (*payload == 0x00) ++payload;
679 ++payload;
680 if (*payload != 0x02) break;
681 payload += 2 + payload[1];
682 }
683 else
684 payload = 0;
685
686 }
687
688 return(rc);
689}
690
691static int videoguard2_card_info(struct s_reader * reader)
692{
693 /* info is displayed in init, or when processing info */
694 cs_log("%s card detected", reader->label);
695 cs_log("type: %s", reader->card_desc );
696 vg2_read_tiers(reader);
697 return OK;
698}
699
700void reader_videoguard2(struct s_cardsystem *ph)
701{
702 ph->do_emm=videoguard2_do_emm;
703 ph->do_ecm=videoguard2_do_ecm;
704 ph->card_info=videoguard2_card_info;
705 ph->card_init=videoguard2_card_init;
706 ph->get_emm_type=videoguard_get_emm_type;
707 ph->get_emm_filter=videoguard_get_emm_filter;
708 ph->caids[0]=0x09;
709 ph->desc="videoguard2";
710}
Note: See TracBrowser for help on using the repository browser.