1 | #include "globals.h"
|
---|
2 | #ifdef READER_VIDEOGUARD
|
---|
3 | #include "reader-common.h"
|
---|
4 | #include "reader-videoguard-common.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | static int32_t vg1_do_cmd(struct s_reader *reader, const unsigned char *ins, const unsigned char *txbuff, unsigned char *rxbuff, unsigned char *cta_res)
|
---|
8 | {
|
---|
9 | uint16_t cta_lr;
|
---|
10 | unsigned char ins2[5];
|
---|
11 | memcpy(ins2, ins, 5);
|
---|
12 | unsigned char len = 0;
|
---|
13 | len = ins2[4];
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | if (txbuff == NULL) {
|
---|
18 | if (!write_cmd_vg(ins2, NULL) || !status_ok(cta_res + len)) {
|
---|
19 | return -1;
|
---|
20 | }
|
---|
21 | if(rxbuff != NULL){
|
---|
22 | memcpy(rxbuff, ins2, 5);
|
---|
23 | memcpy(rxbuff + 5, cta_res, len);
|
---|
24 | memcpy(rxbuff + 5 + len, cta_res + len, 2);
|
---|
25 | }
|
---|
26 | } else {
|
---|
27 | if (!write_cmd_vg(ins2, (uchar *) txbuff) || !status_ok(cta_res)) {
|
---|
28 | return -2;
|
---|
29 | }
|
---|
30 | if(rxbuff != NULL){
|
---|
31 | memcpy(rxbuff, ins2, 5);
|
---|
32 | memcpy(rxbuff + 5, txbuff, len);
|
---|
33 | memcpy(rxbuff + 5 + len, cta_res, 2);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | return len;
|
---|
38 | }
|
---|
39 |
|
---|
40 | static void read_tiers(struct s_reader *reader)
|
---|
41 | {
|
---|
42 | def_resp;
|
---|
43 | // const unsigned char ins2a[5] = { 0x48, 0x2a, 0x00, 0x00, 0x00 };
|
---|
44 | int32_t l;
|
---|
45 |
|
---|
46 | // return; // Not working at present so just do nothing
|
---|
47 |
|
---|
48 | // l = vg1_do_cmd(reader, ins2a, NULL, NULL, cta_res);
|
---|
49 | // if (l < 0 || !status_ok(cta_res + l))
|
---|
50 | // {
|
---|
51 | // return;
|
---|
52 | // }
|
---|
53 | unsigned char ins76[5] = { 0x48, 0x76, 0x00, 0x00, 0x00 };
|
---|
54 | ins76[3] = 0x7f;
|
---|
55 | ins76[4] = 2;
|
---|
56 | if (!write_cmd_vg(ins76, NULL) || !status_ok(cta_res + 2)) {
|
---|
57 | return;
|
---|
58 | }
|
---|
59 | ins76[3] = 0;
|
---|
60 | ins76[4] = 0x0a;
|
---|
61 | int32_t num = cta_res[1];
|
---|
62 | int32_t i;
|
---|
63 |
|
---|
64 | cs_clear_entitlement(reader); //reset the entitlements
|
---|
65 |
|
---|
66 | for (i = 0; i < num; i++) {
|
---|
67 | ins76[2] = i;
|
---|
68 | l = vg1_do_cmd(reader, ins76, NULL, NULL, cta_res);
|
---|
69 | if (l < 0 || !status_ok(cta_res + l)) {
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | if (cta_res[2] == 0 && cta_res[3] == 0) {
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | uint16_t tier_id = (cta_res[2] << 8) | cta_res[3];
|
---|
76 | // add entitlements to list
|
---|
77 | struct tm timeinfo;
|
---|
78 | memset(&timeinfo, 0, sizeof(struct tm));
|
---|
79 | rev_date_calc_tm(&cta_res[4],&timeinfo,reader->card_baseyear);
|
---|
80 | char tiername[83];
|
---|
81 | cs_add_entitlement(reader, reader->caid, b2ll(4, reader->prid[0]), tier_id, 0, 0, mktime(&timeinfo), 4);
|
---|
82 | rdr_log(reader, "tier: %04x, expiry date: %04d/%02d/%02d-%02d:%02d:%02d %s",tier_id,timeinfo.tm_year+1900,timeinfo.tm_mon+1,timeinfo.tm_mday,timeinfo.tm_hour,timeinfo.tm_min,timeinfo.tm_sec,get_tiername(tier_id, reader->caid, tiername));
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | static int32_t videoguard1_card_init(struct s_reader *reader, ATR *newatr)
|
---|
87 | {
|
---|
88 |
|
---|
89 | get_hist;
|
---|
90 | /* 40 B0 09 4A 50 01 4E 5A */
|
---|
91 | if ((hist_size < 7) || (hist[1] != 0xB0) || (hist[3] != 0x4A) || (hist[4] != 0x50)) {
|
---|
92 | return ERROR;
|
---|
93 | }
|
---|
94 |
|
---|
95 | get_atr;
|
---|
96 | def_resp;
|
---|
97 |
|
---|
98 | /* set information on the card stored in reader-videoguard-common.c */
|
---|
99 | set_known_card_info(reader,atr,&atr_size);
|
---|
100 |
|
---|
101 | if((reader->ndsversion != NDS1) && ((reader->card_system_version != NDS1) || (reader->ndsversion != NDSAUTO))) {
|
---|
102 | /* known ATR and not NDS1
|
---|
103 | or unknown ATR and not forced to NDS1
|
---|
104 | or known NDS1 ATR and forced to another NDS version
|
---|
105 | ... probably not NDS1 */
|
---|
106 | return ERROR;
|
---|
107 | }
|
---|
108 |
|
---|
109 | rdr_log(reader, "type: %s, baseyear: %i", reader->card_desc, reader->card_baseyear);
|
---|
110 | if(reader->ndsversion == NDS1){
|
---|
111 | rdr_log(reader, "forced to NDS1+");
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* NDS1 Class 48 only cards only need a very basic initialisation
|
---|
115 | NDS1 Class 48 only cards do not respond to vg1_do_cmd(ins7416)
|
---|
116 | nor do they return list of valid command therefore do not even try
|
---|
117 | NDS1 Class 48 only cards need to be told the length as (48, ins, 00, 80, 01)
|
---|
118 | does not return the length */
|
---|
119 |
|
---|
120 | int32_t l = 0;
|
---|
121 | unsigned char buff[256];
|
---|
122 | memset(buff, 0, sizeof(buff));
|
---|
123 |
|
---|
124 | /* Try to get the boxid from the card, even if BoxID specified in the config file
|
---|
125 | also used to check if it is an NDS1 card as the returned information will
|
---|
126 | not be encrypted if it is an NDS1 card */
|
---|
127 |
|
---|
128 | static const unsigned char ins36[5] = { 0x48, 0x36, 0x00, 0x00, 0x90 };
|
---|
129 | unsigned char boxID[4];
|
---|
130 | int32_t boxidOK = 0;
|
---|
131 | l = vg1_do_cmd(reader, ins36, NULL, buff, cta_res);
|
---|
132 | if (buff[7] > 0x0F) {
|
---|
133 | rdr_log(reader, "class48 ins36: encrypted - therefore not an NDS1 card");
|
---|
134 | return ERROR;
|
---|
135 | } else {
|
---|
136 | /* skipping the initial fixed fields: cmdecho (4) + length (1) + encr/rev++ (4) */
|
---|
137 | int32_t i = 9;
|
---|
138 | int32_t gotUA = 0;
|
---|
139 | while (i < l) {
|
---|
140 | if (!gotUA && buff[i] < 0xF0) { /* then we guess that the next 4 bytes is the UA */
|
---|
141 | gotUA = 1;
|
---|
142 | i += 4;
|
---|
143 | } else {
|
---|
144 | switch (buff[i]) { /* object length vary depending on type */
|
---|
145 | case 0x00: /* padding */
|
---|
146 | {
|
---|
147 | i += 1;
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | case 0xEF: /* card status */
|
---|
151 | {
|
---|
152 | i += 3;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | case 0xD1:
|
---|
156 | {
|
---|
157 | i += 4;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | case 0xDF: /* next server contact */
|
---|
161 | {
|
---|
162 | i += 5;
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | case 0xF3: /* boxID */
|
---|
166 | {
|
---|
167 | memcpy(&boxID, &buff[i + 1], sizeof(boxID));
|
---|
168 | boxidOK = 1;
|
---|
169 | i += 5;
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | case 0xF6:
|
---|
173 | {
|
---|
174 | i += 6;
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | case 0xFC: /* No idea seems to with with NDS1 */
|
---|
178 | {
|
---|
179 | i += 14;
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | case 0x01: /* date & time */
|
---|
183 | {
|
---|
184 | i += 7;
|
---|
185 | break;
|
---|
186 | }
|
---|
187 | case 0xFA:
|
---|
188 | {
|
---|
189 | i += 9;
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | case 0x5E:
|
---|
193 | case 0x67: /* signature */
|
---|
194 | case 0xDE:
|
---|
195 | case 0xE2:
|
---|
196 | case 0xE9: /* tier dates */
|
---|
197 | case 0xF8: /* Old PPV Event Record */
|
---|
198 | case 0xFD:
|
---|
199 | {
|
---|
200 | i += buff[i + 1] + 2; /* skip length + 2 bytes (type and length) */
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | default: /* default to assume a length byte */
|
---|
204 | {
|
---|
205 | rdr_log(reader, "class48 ins36: returned unknown type=0x%02X - parsing may fail", buff[i]);
|
---|
206 | i += buff[i + 1] + 2;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | // rdr_log(reader, "calculated BoxID: %02X%02X%02X%02X", boxID[0], boxID[1], boxID[2], boxID[3]);
|
---|
214 |
|
---|
215 | /* the boxid is specified in the config */
|
---|
216 | if (reader->boxid > 0) {
|
---|
217 | int32_t i;
|
---|
218 | for (i = 0; i < 4; i++) {
|
---|
219 | boxID[i] = (reader->boxid >> (8 * (3 - i))) % 0x100;
|
---|
220 | }
|
---|
221 | // rdr_log(reader, "config BoxID: %02X%02X%02X%02X", boxID[0], boxID[1], boxID[2], boxID[3]);
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (!boxidOK) {
|
---|
225 | rdr_log(reader, "no boxID available");
|
---|
226 | return ERROR;
|
---|
227 | }
|
---|
228 |
|
---|
229 | // Send BoxID
|
---|
230 | static const unsigned char ins4C[5] = { 0x48, 0x4C, 0x00, 0x00, 0x09 };
|
---|
231 | unsigned char payload4C[9] = { 0, 0, 0, 0, 3, 0, 0, 0, 4 };
|
---|
232 | memcpy(payload4C, boxID, 4);
|
---|
233 | if (!write_cmd_vg(ins4C, payload4C) || !status_ok(cta_res + l)) {
|
---|
234 | rdr_log(reader, "class48 ins4C: sending boxid failed");
|
---|
235 | return ERROR;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static const unsigned char ins58[5] = { 0x48, 0x58, 0x00, 0x00, 0x17 };
|
---|
239 | l = vg1_do_cmd(reader, ins58, NULL, buff, cta_res);
|
---|
240 | if (l < 0) {
|
---|
241 | rdr_log(reader, "class48 ins58: failed");
|
---|
242 | return ERROR;
|
---|
243 | }
|
---|
244 |
|
---|
245 | memset(reader->hexserial, 0, 8);
|
---|
246 | memcpy(reader->hexserial + 2, cta_res + 1, 4);
|
---|
247 | memcpy(reader->sa, cta_res + 1, 3);
|
---|
248 | // reader->caid = cta_res[24] * 0x100 + cta_res[25];
|
---|
249 | /* Force caid until can figure out how to get it */
|
---|
250 | reader->caid = 0x9 * 0x100 + 0x69;
|
---|
251 |
|
---|
252 | /* we have one provider, 0x0000 */
|
---|
253 | reader->nprov = 1;
|
---|
254 | memset(reader->prid, 0x00, sizeof(reader->prid));
|
---|
255 |
|
---|
256 | rdr_log_sensitive(reader, "type: VideoGuard, caid: %04X, serial: {%02X%02X%02X%02X}, BoxID: {%02X%02X%02X%02X}",
|
---|
257 | reader->caid, reader->hexserial[2], reader->hexserial[3], reader->hexserial[4], reader->hexserial[5],
|
---|
258 | boxID[0], boxID[1], boxID[2], boxID[3]);
|
---|
259 | rdr_log(reader, "ready for requests - this is in testing please send -d 255 logs");
|
---|
260 |
|
---|
261 | return OK;
|
---|
262 | }
|
---|
263 |
|
---|
264 | static int32_t videoguard1_do_ecm(struct s_reader * reader, const ECM_REQUEST *er, struct s_ecm_answer *ea)
|
---|
265 | {
|
---|
266 | unsigned char cta_res[CTA_RES_LEN];
|
---|
267 | unsigned char ins40[5] = { 0x48, 0x40, 0x00, 0x80, 0xFF };
|
---|
268 | static const unsigned char ins54[5] = { 0x48, 0x54, 0x00, 0x00, 0x0D };
|
---|
269 | int32_t posECMpart2 = er->ecm[6] + 7;
|
---|
270 | int32_t lenECMpart2 = er->ecm[posECMpart2];
|
---|
271 | unsigned char tbuff[264];
|
---|
272 | unsigned char rbuff[264];
|
---|
273 | memcpy(&tbuff[0], &(er->ecm[posECMpart2 + 1]), lenECMpart2);
|
---|
274 | ins40[4] = lenECMpart2;
|
---|
275 | int32_t l;
|
---|
276 | l = vg1_do_cmd(reader, ins40, tbuff, NULL, cta_res);
|
---|
277 | if (l > 0 && status_ok(cta_res)) {
|
---|
278 | l = vg1_do_cmd(reader, ins54, NULL, rbuff, cta_res);
|
---|
279 | if (l > 0 && status_ok(cta_res + l)) {
|
---|
280 | if (!cw_is_valid(rbuff+5)) //sky cards report 90 00 = ok but send cw = 00 when channel not subscribed
|
---|
281 | {
|
---|
282 | rdr_log(reader, "class48 ins54 status 90 00 but cw=00 -> channel not subscribed");
|
---|
283 | return ERROR;
|
---|
284 | }
|
---|
285 |
|
---|
286 | if(er->ecm[0]&1) {
|
---|
287 | memset(ea->cw+0, 0, 8);
|
---|
288 | memcpy(ea->cw+8, rbuff + 5, 8);
|
---|
289 | } else {
|
---|
290 | memcpy(ea->cw+0, rbuff + 5, 8);
|
---|
291 | memset(ea->cw+8, 0, 8);
|
---|
292 | }
|
---|
293 | return OK;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | rdr_log(reader, "class48 ins54 (%d) status not ok %02x %02x", l, cta_res[0], cta_res[1]);
|
---|
297 | return ERROR;
|
---|
298 | }
|
---|
299 |
|
---|
300 | static int32_t videoguard1_do_emm(struct s_reader *reader, EMM_PACKET * ep)
|
---|
301 | {
|
---|
302 | return videoguard_do_emm(reader, ep, 0x48, read_tiers, vg1_do_cmd);
|
---|
303 | }
|
---|
304 |
|
---|
305 | static int32_t videoguard1_card_info(struct s_reader *reader)
|
---|
306 | {
|
---|
307 | /* info is displayed in init, or when processing info */
|
---|
308 | rdr_log(reader, "card detected");
|
---|
309 | rdr_log(reader, "type: %s", reader->card_desc);
|
---|
310 | read_tiers(reader);
|
---|
311 | return OK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | void reader_videoguard1(struct s_cardsystem *ph)
|
---|
315 | {
|
---|
316 | ph->do_emm=videoguard1_do_emm;
|
---|
317 | ph->do_ecm=videoguard1_do_ecm;
|
---|
318 | ph->card_info=videoguard1_card_info;
|
---|
319 | ph->card_init=videoguard1_card_init;
|
---|
320 | ph->get_emm_type=videoguard_get_emm_type;
|
---|
321 | ph->get_emm_filter=videoguard_get_emm_filter;
|
---|
322 | ph->caids[0]=0x09;
|
---|
323 | ph->desc="videoguard1";
|
---|
324 | }
|
---|
325 | #endif
|
---|