1 | #include "globals.h"
|
---|
2 | #include "reader-common.h"
|
---|
3 | #include "cscrypt/idea.h"
|
---|
4 | #include <termios.h>
|
---|
5 | #include <unistd.h>
|
---|
6 |
|
---|
7 | // Card Status checks
|
---|
8 | #define HAS_CW() ((reader->cam_state[2]&6)==6)
|
---|
9 | #define RENEW_SESSIONKEY() ((reader->cam_state[0]&128)==128 || (reader->cam_state[0]&64)==64 || (reader->cam_state[0]&32)==32 || (reader->cam_state[2]&8)==8)
|
---|
10 | #define SENDDATETIME() (reader->cam_state[0]&8)
|
---|
11 | // Datatypes
|
---|
12 | #define DT01 0x01
|
---|
13 | #define IRDINFO 0x00
|
---|
14 | #define TIERS 0x05
|
---|
15 | #define DT06 0x06
|
---|
16 | #define CAMDATA 0x08
|
---|
17 |
|
---|
18 | #define MAX_REC 20
|
---|
19 | #define SYSTEM_NAGRA 0x1800
|
---|
20 | #define SYSTEM_MASK 0xFF00
|
---|
21 |
|
---|
22 |
|
---|
23 | static time_t tier_date(ulong date, char *buf, int l)
|
---|
24 | {
|
---|
25 | time_t ut=870393600L+date*(24*3600);
|
---|
26 | if (buf)
|
---|
27 | {
|
---|
28 | struct tm *t;
|
---|
29 | t=gmtime(&ut);
|
---|
30 | snprintf(buf, l, "%04d/%02d/%02d", t->tm_year+1900, t->tm_mon+1, t->tm_mday);
|
---|
31 | }
|
---|
32 | return(ut);
|
---|
33 | }
|
---|
34 |
|
---|
35 | static int do_cmd(struct s_reader * reader, unsigned char cmd, int ilen, unsigned char res, int rlen, unsigned char *data, unsigned char * cta_res, unsigned short * p_cta_lr)
|
---|
36 | {
|
---|
37 | /*
|
---|
38 | here we build the command related to the protocol T1 for ROM142 or T14 for ROM181
|
---|
39 | the only different that i know is the command length byte msg[4], this msg[4]+=1 by a ROM181 smartcard (_nighti_)
|
---|
40 | one example for the cmd$C0
|
---|
41 | T14 protocol: 01 A0 CA 00 00 03 C0 00 06 91
|
---|
42 | T1 protocol: 21 00 08 A0 CA 00 00 02 C0 00 06 87
|
---|
43 | */
|
---|
44 | int msglen=ilen+6;
|
---|
45 | unsigned char msg[msglen];
|
---|
46 | static char nagra_head[] = {0xA0, 0xCA, 0x00, 0x00};
|
---|
47 |
|
---|
48 | memset(msg, 0, msglen);
|
---|
49 | memcpy(msg,nagra_head,4);
|
---|
50 | msg[4] = ilen;
|
---|
51 | msg[5] = cmd;
|
---|
52 | int dlen=ilen-2;
|
---|
53 | msg[6] = dlen;
|
---|
54 | if(data && dlen>0) memcpy(msg+7,data,dlen);
|
---|
55 | msg[dlen+7] = rlen;
|
---|
56 | if (dlen<0)
|
---|
57 | {
|
---|
58 | cs_debug("[nagra-reader] invalid data length encountered");
|
---|
59 | return ERROR;
|
---|
60 | }
|
---|
61 | if (reader->is_pure_nagra==1)
|
---|
62 | {
|
---|
63 | msg[4]+=1;
|
---|
64 | }
|
---|
65 | if(!reader_cmd2icc(reader, msg,msglen, cta_res, p_cta_lr))
|
---|
66 | {
|
---|
67 | cs_sleepms(5);
|
---|
68 | if(cta_res[0]!=res)
|
---|
69 | {
|
---|
70 | cs_debug("[nagra-reader] result not expected (%02x != %02x)",cta_res[0],res);
|
---|
71 | return ERROR;
|
---|
72 | }
|
---|
73 | if((*p_cta_lr-2)!=rlen)
|
---|
74 | {
|
---|
75 | cs_debug("[nagra-reader] result length expected (%d != %d)",(*p_cta_lr-2),rlen);
|
---|
76 | return ERROR;
|
---|
77 | }
|
---|
78 | return *p_cta_lr;
|
---|
79 | }
|
---|
80 | return ERROR;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static void ReverseMem(unsigned char *vIn, int len)
|
---|
84 | {
|
---|
85 | unsigned char temp;
|
---|
86 | int i;
|
---|
87 | for(i=0; i < (len/2); i++)
|
---|
88 | {
|
---|
89 | temp = vIn[i];
|
---|
90 | vIn[i] = vIn[len-i-1];
|
---|
91 | vIn[len-i-1] = temp;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | static void Signature(unsigned char *sig, const unsigned char *vkey,const unsigned char *msg, int len)
|
---|
96 | {
|
---|
97 | IDEA_KEY_SCHEDULE ks;
|
---|
98 | unsigned char v[8];
|
---|
99 | unsigned char b200[16];
|
---|
100 | unsigned char b0f0[8];
|
---|
101 | memcpy(b200,vkey,sizeof(b200));
|
---|
102 | int i;
|
---|
103 | int j;
|
---|
104 | for(i=0; i<len; i+=8)
|
---|
105 | {
|
---|
106 | idea_set_encrypt_key(b200,&ks);
|
---|
107 | memset(v,0,sizeof(v));
|
---|
108 | idea_cbc_encrypt(msg+i,b0f0,8,&ks,v,IDEA_DECRYPT);
|
---|
109 | for(j=7; j>=0; j--) b0f0[j]^=msg[i+j];
|
---|
110 | memcpy(b200+0,b0f0,8);
|
---|
111 | memcpy(b200+8,b0f0,8);
|
---|
112 | }
|
---|
113 | memcpy(sig,b0f0,8);
|
---|
114 | return;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static int CamStateRequest(struct s_reader * reader)
|
---|
118 | {
|
---|
119 | def_resp;
|
---|
120 | if(do_cmd(reader, 0xC0,0x02,0xB0,0x06,NULL,cta_res,&cta_lr))
|
---|
121 | {
|
---|
122 | memcpy(reader->cam_state,cta_res+3,3);
|
---|
123 | cs_debug("[nagra-reader] Camstate: %s",cs_hexdump (1, reader->cam_state, 3));
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | cs_debug("[nagra-reader] CamStateRequest failed");
|
---|
128 | return ERROR;
|
---|
129 | }
|
---|
130 | return OK;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void DateTimeCMD(struct s_reader * reader)
|
---|
134 | {
|
---|
135 | def_resp;
|
---|
136 | if (!do_cmd(reader, 0xC8,0x02,0xB8,0x06,NULL,cta_res,&cta_lr))
|
---|
137 | {
|
---|
138 | cs_debug("[nagra-reader] DateTimeCMD failed!");
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|
142 |
|
---|
143 | static int NegotiateSessionKey_Tiger(struct s_reader * reader)
|
---|
144 | {
|
---|
145 | def_resp;
|
---|
146 | unsigned char vFixed[] = {0,1,2,3,0x11};
|
---|
147 | unsigned char parte_fija[120];
|
---|
148 | unsigned char parte_variable[88];
|
---|
149 | unsigned char d1_rsa_modulo[88];
|
---|
150 | unsigned char d2_data[88];
|
---|
151 | unsigned char sign1[8];
|
---|
152 | unsigned char sk[16];
|
---|
153 | unsigned char tmp[104];
|
---|
154 | unsigned char idea_sig[16];
|
---|
155 | unsigned char random[88];
|
---|
156 |
|
---|
157 | if(!do_cmd(reader, 0xd1,0x02,0x51,0xd2,NULL,cta_res,&cta_lr))
|
---|
158 | {
|
---|
159 | cs_debug("[nagra-reader] CMD$D1 failed");
|
---|
160 | return ERROR;
|
---|
161 | }
|
---|
162 |
|
---|
163 | BN_CTX *ctx = BN_CTX_new();
|
---|
164 | BIGNUM *bnN = BN_CTX_get(ctx);
|
---|
165 | BIGNUM *bnE = BN_CTX_get(ctx);
|
---|
166 | BIGNUM *bnCT = BN_CTX_get(ctx);
|
---|
167 | BIGNUM *bnPT = BN_CTX_get(ctx);
|
---|
168 | BN_bin2bn(reader->rsa_mod, 120, bnN);
|
---|
169 | BN_bin2bn(vFixed+4, 1, bnE);
|
---|
170 | BN_bin2bn(&cta_res[90], 120, bnCT);
|
---|
171 | BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx);
|
---|
172 | memset(parte_fija, 0, 120);
|
---|
173 | BN_bn2bin(bnPT, parte_fija + (120-BN_num_bytes(bnPT)));
|
---|
174 | BN_CTX_end(ctx);
|
---|
175 | BN_CTX_free (ctx);
|
---|
176 |
|
---|
177 | cs_debug("[nagra-reader] ---------- SIG CHECK ---------------------");
|
---|
178 | memset(tmp,0, 104);
|
---|
179 | memcpy(tmp+4, parte_fija+11, 100);
|
---|
180 | memset(idea_sig, 0x37, 16);
|
---|
181 | Signature(sign1, idea_sig, tmp, 104);
|
---|
182 | cs_debug("[nagra-reader] sign1: %s", cs_hexdump (0, sign1, 8));
|
---|
183 | cs_debug("[nagra-reader] sign2: %s", cs_hexdump (0, parte_fija+111, 8));
|
---|
184 | if (!memcmp (parte_fija+111, sign1, 8)==0)
|
---|
185 | {
|
---|
186 | cs_debug("[nagra-reader] signature check nok");
|
---|
187 | cs_debug("[nagra-reader] ------------------------------------------");
|
---|
188 | return ERROR;
|
---|
189 | }
|
---|
190 | cs_debug("[nagra-reader] signature check ok");
|
---|
191 | cs_debug("[nagra-reader] ------------------------------------------");
|
---|
192 |
|
---|
193 | memcpy(reader->hexserial+2, parte_fija+15, 4);
|
---|
194 | memcpy(reader->irdId, parte_fija+19, 4);
|
---|
195 | memcpy(d1_rsa_modulo,parte_fija+23,88);
|
---|
196 |
|
---|
197 | ReverseMem(cta_res+2, 88);
|
---|
198 | BN_CTX *ctx1 = BN_CTX_new();
|
---|
199 | BIGNUM *bnN1 = BN_CTX_get(ctx1);
|
---|
200 | BIGNUM *bnE1 = BN_CTX_get(ctx1);
|
---|
201 | BIGNUM *bnCT1 = BN_CTX_get(ctx1);
|
---|
202 | BIGNUM *bnPT1 = BN_CTX_get(ctx1);
|
---|
203 | BN_bin2bn(d1_rsa_modulo, 88, bnN1);
|
---|
204 | BN_bin2bn(vFixed+4, 1, bnE1);
|
---|
205 | BN_bin2bn(cta_res+2, 88, bnCT1);
|
---|
206 | BN_mod_exp(bnPT1, bnCT1, bnE1, bnN1, ctx1);
|
---|
207 | memset(parte_variable, 0, 88);
|
---|
208 | BN_bn2bin(bnPT1, parte_variable + (88-BN_num_bytes(bnPT1)));
|
---|
209 | BN_CTX_end(ctx1);
|
---|
210 | BN_CTX_free (ctx1);
|
---|
211 |
|
---|
212 | reader->prid[0][0]=0x00;
|
---|
213 | reader->prid[0][1]=0x00;
|
---|
214 | reader->prid[0][2]=parte_variable[73];
|
---|
215 | reader->prid[0][3]=parte_variable[74];
|
---|
216 | reader->caid[0] =(SYSTEM_NAGRA|parte_variable[76]);
|
---|
217 | memcpy(sk,&parte_variable[79],8);
|
---|
218 | memcpy(sk+8,&parte_variable[79],8);
|
---|
219 | cs_ri_log(reader, "type: NAGRA, caid: %04X, IRD ID: %s",reader->caid[0], cs_hexdump (1,reader->irdId,4));
|
---|
220 | cs_ri_log(reader, "ProviderID: %s",cs_hexdump (1,reader->prid[0],4));
|
---|
221 |
|
---|
222 | memset(random, 0, 88);
|
---|
223 | memcpy(random, sk,16);
|
---|
224 | ReverseMem(random, 88);
|
---|
225 |
|
---|
226 |
|
---|
227 | BN_CTX *ctx3 = BN_CTX_new();
|
---|
228 | BIGNUM *bnN3 = BN_CTX_get(ctx3);
|
---|
229 | BIGNUM *bnE3 = BN_CTX_get(ctx3);
|
---|
230 | BIGNUM *bnCT3 = BN_CTX_get(ctx3);
|
---|
231 | BIGNUM *bnPT3 = BN_CTX_get(ctx3);
|
---|
232 | BN_bin2bn(d1_rsa_modulo, 88, bnN3);
|
---|
233 | BN_bin2bn(vFixed+4, 1, bnE3);
|
---|
234 | BN_bin2bn(random, 88, bnCT3);
|
---|
235 | BN_mod_exp(bnPT3, bnCT3, bnE3, bnN3, ctx3);
|
---|
236 | memset(d2_data, 0, 88);
|
---|
237 | BN_bn2bin(bnPT3, d2_data + (88-BN_num_bytes(bnPT3)));
|
---|
238 | BN_CTX_end(ctx3);
|
---|
239 | BN_CTX_free (ctx3);
|
---|
240 | ReverseMem(d2_data, 88);
|
---|
241 |
|
---|
242 | if(!do_cmd(reader, 0xd2,0x5a,0x52,0x03, d2_data,cta_res,&cta_lr))
|
---|
243 | {
|
---|
244 | cs_debug("[nagra-reader] CMD$D2 failed");
|
---|
245 | return ERROR;
|
---|
246 | }
|
---|
247 | if (cta_res[2] == 0x00)
|
---|
248 | {
|
---|
249 | memcpy(reader->sessi,sk,16);
|
---|
250 | IDEA_KEY_SCHEDULE ks;
|
---|
251 | idea_set_encrypt_key(reader->sessi,&ks);
|
---|
252 | idea_set_decrypt_key(&ks,&reader->ksSession);
|
---|
253 | cs_debug("[nagra-reader] session key negotiated");
|
---|
254 | return OK;
|
---|
255 | }
|
---|
256 | cs_ri_log(reader, "Negotiate sessionkey was not successfull! Please check tivusat rsa key");
|
---|
257 | return ERROR;
|
---|
258 |
|
---|
259 | }
|
---|
260 |
|
---|
261 | static int NegotiateSessionKey(struct s_reader * reader)
|
---|
262 | {
|
---|
263 | def_resp;
|
---|
264 | unsigned char cmd2b[] = {0x21, 0x40, 0x48, 0xA0, 0xCA, 0x00, 0x00, 0x43, 0x2B, 0x40, 0x1C, 0x54, 0xd1, 0x26, 0xe7, 0xe2, 0x40, 0x20, 0xd1, 0x66, 0xf4, 0x18, 0x97, 0x9d, 0x5f, 0x16, 0x8f, 0x7f, 0x7a, 0x55, 0x15, 0x82, 0x31, 0x14, 0x06, 0x57, 0x1a, 0x3f, 0xf0, 0x75, 0x62, 0x41, 0xc2, 0x84, 0xda, 0x4c, 0x2e, 0x84, 0xe9, 0x29, 0x13, 0x81, 0xee, 0xd6, 0xa9, 0xf5, 0xe9, 0xdb, 0xaf, 0x22, 0x51, 0x3d, 0x44, 0xb3, 0x20, 0x83, 0xde, 0xcb, 0x5f, 0x35, 0x2b, 0xb0, 0xce, 0x70, 0x02, 0x00};
|
---|
265 | unsigned char negot[64];
|
---|
266 | unsigned char tmp[64];
|
---|
267 | unsigned char idea1[16];
|
---|
268 | unsigned char idea2[16];
|
---|
269 | unsigned char sign1[8];
|
---|
270 | unsigned char sign2[8];
|
---|
271 |
|
---|
272 | if (reader->is_tiger)
|
---|
273 | {
|
---|
274 | if (!NegotiateSessionKey_Tiger(reader))
|
---|
275 | {
|
---|
276 | cs_debug("[nagra-reader] NegotiateSessionKey_Tiger failed");
|
---|
277 | return ERROR;
|
---|
278 | }
|
---|
279 | return OK;
|
---|
280 | }
|
---|
281 | if (!reader->has_dt08) // if we have no valid dt08 calc then we use rsa from config and hexserial for calc of sessionkey
|
---|
282 | {
|
---|
283 | memcpy(reader->plainDT08RSA, reader->rsa_mod, 64);
|
---|
284 | memcpy(reader->signature,reader->nagra_boxkey, 8);
|
---|
285 | }
|
---|
286 | if(!do_cmd(reader, 0x2a,0x02,0xaa,0x42,NULL,cta_res,&cta_lr))
|
---|
287 | {
|
---|
288 | cs_debug("[nagra-reader] CMD$2A failed");
|
---|
289 | return ERROR;
|
---|
290 | }
|
---|
291 |
|
---|
292 | // RSA decrypt of cmd$2a data, result is stored in "negot"
|
---|
293 | ReverseMem(cta_res+2, 64);
|
---|
294 | unsigned char vFixed[] = {0,1,2,3};
|
---|
295 | BN_CTX *ctx = BN_CTX_new();
|
---|
296 | BIGNUM *bnN = BN_CTX_get(ctx);
|
---|
297 | BIGNUM *bnE = BN_CTX_get(ctx);
|
---|
298 | BIGNUM *bnCT = BN_CTX_get(ctx);
|
---|
299 | BIGNUM *bnPT = BN_CTX_get(ctx);
|
---|
300 | BN_bin2bn(reader->plainDT08RSA, 64, bnN);
|
---|
301 | BN_bin2bn(vFixed+3, 1, bnE);
|
---|
302 | BN_bin2bn(cta_res+2, 64, bnCT);
|
---|
303 | BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx);
|
---|
304 | memset(negot, 0, 64);
|
---|
305 | BN_bn2bin(bnPT, negot + (64-BN_num_bytes(bnPT)));
|
---|
306 |
|
---|
307 | memcpy(tmp, negot, 64);
|
---|
308 | ReverseMem(tmp, 64);
|
---|
309 |
|
---|
310 | // build sessionkey
|
---|
311 | // first halve is IDEA Hashed in chuncs of 8 bytes using the Signature1 from dt08 calc, CamID-Inv.CamID(16 bytes key) the results are the First 8 bytes of the Session key
|
---|
312 | memcpy(idea1, reader->signature, 8);
|
---|
313 | memcpy(idea1+8, reader->hexserial+2, 4);
|
---|
314 | idea1[12] = ~reader->hexserial[2]; idea1[13] = ~reader->hexserial[3]; idea1[14] = ~reader->hexserial[4]; idea1[15] = ~reader->hexserial[5];
|
---|
315 |
|
---|
316 | Signature(sign1, idea1, tmp, 32);
|
---|
317 | memcpy(idea2,sign1,8); memcpy(idea2+8,sign1,8);
|
---|
318 | Signature(sign2, idea2, tmp, 32);
|
---|
319 | memcpy(reader->sessi,sign1,8); memcpy(reader->sessi+8,sign2,8);
|
---|
320 |
|
---|
321 | // prepare cmd$2b data
|
---|
322 | BN_bin2bn(negot, 64, bnCT);
|
---|
323 | BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx);
|
---|
324 | memset(cmd2b+10, 0, 64);
|
---|
325 | BN_bn2bin(bnPT, cmd2b+10 + (64-BN_num_bytes(bnPT)));
|
---|
326 | BN_CTX_end(ctx);
|
---|
327 | BN_CTX_free (ctx);
|
---|
328 | ReverseMem(cmd2b+10, 64);
|
---|
329 |
|
---|
330 | IDEA_KEY_SCHEDULE ks;
|
---|
331 | idea_set_encrypt_key(reader->sessi,&ks);
|
---|
332 | idea_set_decrypt_key(&ks,&reader->ksSession);
|
---|
333 |
|
---|
334 | if(!do_cmd(reader, 0x2b,0x42,0xab,0x02, cmd2b+10,cta_res,&cta_lr))
|
---|
335 | {
|
---|
336 | cs_debug("[nagra-reader] CMD$2B failed");
|
---|
337 | return ERROR;
|
---|
338 | }
|
---|
339 |
|
---|
340 | cs_debug("[nagra-reader] session key negotiated");
|
---|
341 |
|
---|
342 | DateTimeCMD(reader);
|
---|
343 |
|
---|
344 | if (!CamStateRequest(reader))
|
---|
345 | {
|
---|
346 | cs_debug("[nagra-reader] CamStateRequest failed");
|
---|
347 | return ERROR;
|
---|
348 | }
|
---|
349 | if RENEW_SESSIONKEY()
|
---|
350 | {
|
---|
351 | cs_ri_log(reader, "Negotiate sessionkey was not successfull! Please check rsa key and boxkey");
|
---|
352 | return ERROR;
|
---|
353 | }
|
---|
354 |
|
---|
355 | return OK;
|
---|
356 | }
|
---|
357 |
|
---|
358 | static void decryptDT08(struct s_reader * reader, unsigned char * cta_res)
|
---|
359 | {
|
---|
360 | unsigned char vFixed[] = {0,1,2,3};
|
---|
361 | unsigned char v[72];
|
---|
362 | unsigned char buf[72];
|
---|
363 | unsigned char sign2[8];
|
---|
364 | unsigned char static_dt08[73];
|
---|
365 | unsigned char camid[4];
|
---|
366 | int i, n;
|
---|
367 | BN_CTX *ctx;
|
---|
368 | BIGNUM *bn_mod, *bn_exp, *bn_data, *bn_res;
|
---|
369 |
|
---|
370 | memcpy(static_dt08, &cta_res[12], 73);
|
---|
371 | // decrypt RSA Part of dt08
|
---|
372 | bn_mod = BN_new ();
|
---|
373 | bn_exp = BN_new ();
|
---|
374 | bn_data = BN_new ();
|
---|
375 | bn_res = BN_new ();
|
---|
376 | ctx= BN_CTX_new();
|
---|
377 | if (ctx == NULL) cs_debug("[nagra-reader] RSA Error in dt08 decrypt");
|
---|
378 | ReverseMem(static_dt08+1, 64);
|
---|
379 | BN_bin2bn (reader->rsa_mod, 64, bn_mod); // rsa modulus
|
---|
380 | BN_bin2bn (vFixed+3, 1, bn_exp); // exponent
|
---|
381 | BN_bin2bn (static_dt08+1, 64, bn_data);
|
---|
382 | BN_mod_exp (bn_res, bn_data, bn_exp, bn_mod, ctx);
|
---|
383 | memset (static_dt08+1, 0, 64);
|
---|
384 | n = BN_bn2bin (bn_res, static_dt08+1);
|
---|
385 | BN_CTX_free (ctx);
|
---|
386 | ReverseMem(static_dt08+1, n);
|
---|
387 |
|
---|
388 | // RSA data can never be bigger than the modulo
|
---|
389 | static_dt08[64] |= static_dt08[0] & 0x80;
|
---|
390 |
|
---|
391 | // IdeaCamKey
|
---|
392 | memcpy (&reader->IdeaCamKey[0], reader->nagra_boxkey, 8);
|
---|
393 | memcpy (&reader->IdeaCamKey[8], reader->irdId, 4);
|
---|
394 | for (i = 0; i < 4; i++)
|
---|
395 | reader->IdeaCamKey[12 + i] = ~reader->irdId[i];
|
---|
396 |
|
---|
397 | // now IDEA decrypt
|
---|
398 | IDEA_KEY_SCHEDULE ks;
|
---|
399 | idea_set_encrypt_key(reader->IdeaCamKey,&ks);
|
---|
400 | idea_set_decrypt_key(&ks,&reader->ksSession);
|
---|
401 | memcpy (&buf[0], static_dt08+1, 64);
|
---|
402 | memcpy (&buf[64], static_dt08+65, 8);
|
---|
403 | memset(v,0,sizeof(v));
|
---|
404 | memset(static_dt08,0,sizeof(static_dt08));
|
---|
405 | idea_cbc_encrypt(buf,static_dt08,72,&reader->ksSession,v,IDEA_DECRYPT);
|
---|
406 |
|
---|
407 | if (reader->swapCW==1)
|
---|
408 | {
|
---|
409 | memset(camid,0xff,4);
|
---|
410 | }
|
---|
411 | else
|
---|
412 | {
|
---|
413 | memcpy(camid, reader->hexserial+2,4);
|
---|
414 | }
|
---|
415 | cs_debug("[nagra-reader] using camid %s for dt08 calc",cs_hexdump (1,camid,4));
|
---|
416 |
|
---|
417 | // Calculate reader->signature
|
---|
418 | memcpy (reader->signature, static_dt08, 8);
|
---|
419 | memset (static_dt08 + 0, 0, 4);
|
---|
420 | memcpy (static_dt08 + 4, camid, 4);
|
---|
421 | Signature(sign2,reader->IdeaCamKey,static_dt08,72);
|
---|
422 |
|
---|
423 | if (memcmp (reader->signature, sign2, 8)==0)
|
---|
424 | {
|
---|
425 | reader->has_dt08=1;
|
---|
426 | memcpy (reader->plainDT08RSA, static_dt08+8, 64);
|
---|
427 | cs_debug("[nagra-reader] DT08 signature check ok");
|
---|
428 | }
|
---|
429 | else
|
---|
430 | {
|
---|
431 | reader->has_dt08=0;
|
---|
432 | cs_debug("[nagra-reader] DT08 signature check nok");
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | static void addProvider(struct s_reader * reader, unsigned char * cta_res)
|
---|
437 | {
|
---|
438 | int i;
|
---|
439 | int toadd=1;
|
---|
440 | for (i=0; i<reader->nprov; i++)
|
---|
441 | {
|
---|
442 | if ((cta_res[7]==reader->prid[i][2]) && (cta_res[8]==reader->prid[i][3]))
|
---|
443 | {
|
---|
444 | toadd = 0;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | if (toadd)
|
---|
448 | {
|
---|
449 | reader->prid[reader->nprov][0]=0;
|
---|
450 | reader->prid[reader->nprov][1]=0;
|
---|
451 | reader->prid[reader->nprov][2]=cta_res[7];
|
---|
452 | reader->prid[reader->nprov][3]=cta_res[8];
|
---|
453 | memcpy(reader->sa[reader->nprov], reader->sa[0], 4);
|
---|
454 | reader->nprov+=1;
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | static int ParseDataType(struct s_reader * reader, unsigned char dt, unsigned char * cta_res, unsigned short cta_lr)
|
---|
459 | {
|
---|
460 | char ds[16], de[16];
|
---|
461 | ushort chid;
|
---|
462 | switch(dt)
|
---|
463 | {
|
---|
464 | case IRDINFO:
|
---|
465 | {
|
---|
466 | reader->prid[0][0]=0;
|
---|
467 | reader->prid[0][1]=0;
|
---|
468 | reader->prid[0][2]=cta_res[7];
|
---|
469 | reader->prid[0][3]=cta_res[8];
|
---|
470 | if ( ((cta_res[7] == 0x34) && (cta_res[8] == 0x11)) || ((cta_res[7] == 0x04) && (cta_res[8] == 0x01))) //provider 3411, 0401 needs cw swap
|
---|
471 | {
|
---|
472 | cs_debug("[nagra-reader] detect provider with swap cw!");
|
---|
473 | reader->swapCW=1;
|
---|
474 | }
|
---|
475 |
|
---|
476 | reader->prid[1][0]=0x00;
|
---|
477 | reader->prid[1][1]=0x00;
|
---|
478 | reader->prid[1][2]=0x00;
|
---|
479 | reader->prid[1][3]=0x00;
|
---|
480 | memcpy(reader->sa[1], reader->sa[0], 4);
|
---|
481 | reader->nprov+=1;
|
---|
482 |
|
---|
483 | reader->caid[0] =(SYSTEM_NAGRA|cta_res[11]);
|
---|
484 | memcpy(reader->irdId,cta_res+14,4);
|
---|
485 | cs_debug("[nagra-reader] type: NAGRA, caid: %04X, IRD ID: %s",reader->caid[0], cs_hexdump (1,reader->irdId,4));
|
---|
486 | cs_debug("[nagra-reader] ProviderID: %s",cs_hexdump (1,reader->prid[0],4));
|
---|
487 | return OK;
|
---|
488 | }
|
---|
489 | case TIERS:
|
---|
490 | if ((cta_lr>33) && (chid=b2i(2, cta_res+11)))
|
---|
491 | {
|
---|
492 | int id=(cta_res[7]*256)|cta_res[8];
|
---|
493 | tier_date(b2i(2, cta_res+20)-0x7f7, ds, 15);
|
---|
494 | tier_date(b2i(2, cta_res+13)-0x7f7, de, 15);
|
---|
495 | cs_ri_log(reader, "|%04X|%04X |%s |%s |", id,chid, ds, de);
|
---|
496 | addProvider(reader, cta_res);
|
---|
497 | }
|
---|
498 | case 0x08:
|
---|
499 | case 0x88: if (cta_res[11] == 0x49) decryptDT08(reader, cta_res);
|
---|
500 | default:
|
---|
501 | return OK;
|
---|
502 | }
|
---|
503 | return ERROR;
|
---|
504 | }
|
---|
505 |
|
---|
506 | static int GetDataType(struct s_reader * reader, unsigned char dt, int len, int shots)
|
---|
507 | {
|
---|
508 | def_resp;
|
---|
509 | int i;
|
---|
510 | for(i=0; i<shots; i++)
|
---|
511 | {
|
---|
512 | if(!do_cmd(reader, 0x22,0x03,0xA2,len,&dt,cta_res,&cta_lr))
|
---|
513 | {
|
---|
514 | cs_debug("[nagra-reader] failed to get datatype %02X",dt);
|
---|
515 | return ERROR;
|
---|
516 | }
|
---|
517 | if((cta_res[2]==0) && (dt != 0x08 || dt != 0x88)) return OK;
|
---|
518 | if(!ParseDataType(reader, dt&0x0F, cta_res, cta_lr)) return ERROR;
|
---|
519 | if ((dt != 0x08 || dt != 0x88) && (cta_res[11] == 0x49)) return OK; //got dt08 data
|
---|
520 | dt|=0x80; // get next item
|
---|
521 | }
|
---|
522 | return OK;
|
---|
523 | }
|
---|
524 |
|
---|
525 | int nagra2_card_init(struct s_reader * reader, ATR newatr)
|
---|
526 | {
|
---|
527 | get_atr;
|
---|
528 | def_resp;
|
---|
529 | memset(reader->rom, 0, 15);
|
---|
530 | reader->nprov = 1;
|
---|
531 | reader->is_pure_nagra = 0;
|
---|
532 | reader->is_tiger = 0;
|
---|
533 | reader->has_dt08 = 0;
|
---|
534 | reader->swapCW = 0;
|
---|
535 | memset(reader->irdId, 0xff, 4);
|
---|
536 | memset(reader->hexserial, 0, 8);
|
---|
537 | reader->caid[0]=SYSTEM_NAGRA;
|
---|
538 |
|
---|
539 | if (memcmp(atr+11, "DNASP", 5)==0)
|
---|
540 | {
|
---|
541 | cs_ri_log(reader, "detect native NAGRA card T1 protocol");
|
---|
542 | memcpy(reader->rom,atr+11,15);
|
---|
543 | }
|
---|
544 | else if (memcmp(atr+11, "TIGER", 5)==0 || (memcmp(atr+11, "NCMED", 5)==0))
|
---|
545 | {
|
---|
546 | cs_ri_log(reader, "detect NAGRA tiger card");
|
---|
547 | memcpy(reader->rom,atr+11,15);
|
---|
548 | reader->is_tiger=1;
|
---|
549 | }
|
---|
550 | else if ((!memcmp(atr+4, "IRDETO", 6)) && ((atr[14]==0x03) && (atr[15]==0x84) && (atr[16]==0x55)))
|
---|
551 | {
|
---|
552 | cs_ri_log(reader, "detect Irdeto tunneled NAGRA card");
|
---|
553 | if(!reader->has_rsa)
|
---|
554 | {
|
---|
555 | cs_ri_log(reader, "switching back to Irdeto mode");
|
---|
556 | return ERROR;
|
---|
557 | }
|
---|
558 | cs_ri_log(reader, "using NAGRA mode");
|
---|
559 | reader->is_pure_nagra=1;
|
---|
560 | if(!do_cmd(reader, 0x10,0x02,0x90,0x11,0,cta_res,&cta_lr))
|
---|
561 | {
|
---|
562 | cs_debug("[nagra-reader] get rom version failed");
|
---|
563 | return ERROR;
|
---|
564 | }
|
---|
565 | memcpy(reader->rom,cta_res+2,15);
|
---|
566 | }
|
---|
567 | else return ERROR;
|
---|
568 |
|
---|
569 | if (!reader->is_tiger)
|
---|
570 | {
|
---|
571 | CamStateRequest(reader);
|
---|
572 | if(!do_cmd(reader, 0x12,0x02,0x92,0x06,0,cta_res,&cta_lr))
|
---|
573 | {
|
---|
574 | cs_debug("[nagra-reader] get Serial failed");
|
---|
575 | return ERROR;
|
---|
576 | }
|
---|
577 | memcpy(reader->hexserial+2, cta_res+2, 4);
|
---|
578 | cs_debug("[nagra-reader] SER: %s", cs_hexdump (1, reader->hexserial+2, 4));
|
---|
579 | memcpy(reader->sa[0], cta_res+2, 2);
|
---|
580 |
|
---|
581 | if(!GetDataType(reader, DT01,0x0E,MAX_REC)) return ERROR;
|
---|
582 | cs_debug("[nagra-reader] DT01 DONE");
|
---|
583 | CamStateRequest(reader);
|
---|
584 | if(!GetDataType(reader, IRDINFO,0x39,MAX_REC)) return ERROR;
|
---|
585 | cs_debug("[nagra-reader] IRDINFO DONE");
|
---|
586 | CamStateRequest(reader);
|
---|
587 | if(!GetDataType(reader, CAMDATA,0x55,10)) return ERROR;
|
---|
588 | cs_debug("[nagra-reader] CAMDATA Done");
|
---|
589 | if(!GetDataType(reader, 0x04,0x44,MAX_REC)) return ERROR;
|
---|
590 | cs_debug("[nagra-reader] DT04 DONE");
|
---|
591 | CamStateRequest(reader);
|
---|
592 |
|
---|
593 | if (!memcmp(reader->rom+5, "181", 3)==0) //dt05 is not supported by rom181
|
---|
594 | {
|
---|
595 | cs_ri_log(reader, "-----------------------------------------");
|
---|
596 | cs_ri_log(reader, "|id |tier |valid from |valid to |");
|
---|
597 | cs_ri_log(reader, "+----+--------+------------+------------+");
|
---|
598 | if(!GetDataType(reader, TIERS,0x57,MAX_REC)) return ERROR;
|
---|
599 | cs_ri_log(reader, "-----------------------------------------");
|
---|
600 | CamStateRequest(reader);
|
---|
601 | }
|
---|
602 |
|
---|
603 | if(!GetDataType(reader, DT06,0x16,MAX_REC)) return ERROR;
|
---|
604 | cs_debug("[nagra-reader] DT06 DONE");
|
---|
605 | CamStateRequest(reader);
|
---|
606 | }
|
---|
607 | if (!NegotiateSessionKey(reader))
|
---|
608 | {
|
---|
609 | cs_debug("[nagra-reader] NegotiateSessionKey failed");
|
---|
610 | return ERROR;
|
---|
611 | }
|
---|
612 | if ((reader->cardmhz != 368) && (reader->is_pure_nagra==0))
|
---|
613 | cs_log("WARNING: For NAGRA2 cards you will have to set 'cardmhz = 368' in oscam.server");
|
---|
614 |
|
---|
615 | return OK;
|
---|
616 | }
|
---|
617 |
|
---|
618 | int nagra2_card_info(struct s_reader * reader)
|
---|
619 | {
|
---|
620 | int i;
|
---|
621 | cs_ri_log(reader, "ROM: %c %c %c %c %c %c %c %c", reader->rom[0], reader->rom[1], reader->rom[2],reader->rom[3], reader->rom[4], reader->rom[5], reader->rom[6], reader->rom[7]);
|
---|
622 | cs_ri_log(reader, "REV: %c %c %c %c %c %c", reader->rom[9], reader->rom[10], reader->rom[11], reader->rom[12], reader->rom[13], reader->rom[14]);
|
---|
623 | cs_ri_log(reader, "SER: %s", cs_hexdump (1, reader->hexserial+2, 4));
|
---|
624 | cs_ri_log(reader, "CAID: %04X",reader->caid[0]);
|
---|
625 | cs_ri_log(reader, "Prv.ID: %s(sysid)",cs_hexdump (1,reader->prid[0],4));
|
---|
626 | for (i=1; i<reader->nprov; i++)
|
---|
627 | {
|
---|
628 | cs_ri_log(reader, "Prv.ID: %s",cs_hexdump (1,reader->prid[i],4));
|
---|
629 | }
|
---|
630 | cs_log("[nagra-reader] ready for requests");
|
---|
631 | return OK;
|
---|
632 | }
|
---|
633 |
|
---|
634 | void nagra2_post_process(struct s_reader * reader)
|
---|
635 | {
|
---|
636 | if (!reader->is_tiger)
|
---|
637 | {
|
---|
638 | CamStateRequest(reader);
|
---|
639 | if RENEW_SESSIONKEY() NegotiateSessionKey(reader);
|
---|
640 | if SENDDATETIME() DateTimeCMD(reader);
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | int nagra2_do_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
645 | {
|
---|
646 | def_resp;
|
---|
647 | if (!reader->is_tiger)
|
---|
648 | {
|
---|
649 | int retry=0;
|
---|
650 | if(!do_cmd(reader, er->ecm[3],er->ecm[4]+2,0x87,0x02, er->ecm+3+2,cta_res,&cta_lr))
|
---|
651 | {
|
---|
652 | cs_debug("[nagra-reader] nagra2_do_ecm failed, retry");
|
---|
653 | cs_sleepms(10);
|
---|
654 | if(!do_cmd(reader, er->ecm[3],er->ecm[4]+2,0x87,0x02, er->ecm+3+2,cta_res,&cta_lr))
|
---|
655 | {
|
---|
656 | cs_debug("[nagra-reader] nagra2_do_ecm failed, retry failed!");
|
---|
657 | return ERROR;
|
---|
658 | }
|
---|
659 |
|
---|
660 | }
|
---|
661 | cs_sleepms(10);
|
---|
662 | while(!CamStateRequest(reader) && retry < 3)
|
---|
663 | {
|
---|
664 | cs_debug("[nagra-reader] CamStateRequest failed, try: %d", retry);
|
---|
665 | retry++;
|
---|
666 | cs_sleepms(10);
|
---|
667 | }
|
---|
668 | if (HAS_CW() && (do_cmd(reader, 0x1C,0x02,0x9C,0x36,NULL,cta_res,&cta_lr)))
|
---|
669 | {
|
---|
670 | unsigned char v[8];
|
---|
671 | memset(v,0,sizeof(v));
|
---|
672 | idea_cbc_encrypt(&cta_res[30],er->cw,8,&reader->ksSession,v,IDEA_DECRYPT);
|
---|
673 | memset(v,0,sizeof(v));
|
---|
674 | idea_cbc_encrypt(&cta_res[4],er->cw+8,8,&reader->ksSession,v,IDEA_DECRYPT);
|
---|
675 | if (reader->swapCW==1)
|
---|
676 | {
|
---|
677 | cs_debug("[nagra-reader] swap cws");
|
---|
678 | unsigned char tt[8];
|
---|
679 | memcpy(&tt[0],&er->cw[0],8);
|
---|
680 | memcpy(&er->cw[0],&er->cw[8],8);
|
---|
681 | memcpy(&er->cw[8],&tt[0],8);
|
---|
682 | }
|
---|
683 | return OK;
|
---|
684 | }
|
---|
685 | }
|
---|
686 | else
|
---|
687 | {
|
---|
688 | //check ECM prov id
|
---|
689 | if (memcmp(&reader->prid[0][2], er->ecm+5, 2))
|
---|
690 | return ERROR;
|
---|
691 |
|
---|
692 | // ecm_data: 80 30 89 D3 87 54 11 10 DA A6 0F 4B 92 05 34 00 ...
|
---|
693 | //serial_data: A0 CA 00 00 8C D3 8A 00 00 00 00 00 10 DA A6 0F .
|
---|
694 | unsigned char ecm_trim[150];
|
---|
695 | memset(ecm_trim, 0, 150);
|
---|
696 | memcpy(&ecm_trim[5], er->ecm+3+2+2, er->ecm[4]+2);
|
---|
697 | if(do_cmd(reader, er->ecm[3],er->ecm[4]+5,0x53,0x16, ecm_trim,cta_res,&cta_lr))
|
---|
698 | {
|
---|
699 | if(cta_res[2] == 0x01)
|
---|
700 | {
|
---|
701 |
|
---|
702 | unsigned char v[8];
|
---|
703 | memset(v,0,sizeof(v));
|
---|
704 | idea_cbc_encrypt(&cta_res[14],er->cw,8,&reader->ksSession,v,IDEA_DECRYPT);
|
---|
705 | memset(v,0,sizeof(v));
|
---|
706 | idea_cbc_encrypt(&cta_res[6],er->cw+8,8,&reader->ksSession,v,IDEA_DECRYPT);
|
---|
707 | return OK;
|
---|
708 | }
|
---|
709 | cs_debug("[nagra-reader] can't decode ecm");
|
---|
710 | return ERROR;
|
---|
711 | }
|
---|
712 | }
|
---|
713 | return ERROR;
|
---|
714 | }
|
---|
715 |
|
---|
716 | int nagra2_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr) //returns TRUE if shared emm matches SA, unique emm matches serial, or global or unknown
|
---|
717 | {
|
---|
718 | switch (ep->emm[0]) {
|
---|
719 | case 0x83:
|
---|
720 | memset(ep->hexserial,0,8);
|
---|
721 | ep->hexserial[0] = ep->emm[5];
|
---|
722 | ep->hexserial[1] = ep->emm[4];
|
---|
723 | ep->hexserial[2] = ep->emm[3];
|
---|
724 | if (ep->emm[7] == 0x10) {
|
---|
725 | ep->type = SHARED;
|
---|
726 | return (!memcmp (rdr->hexserial+2, ep->hexserial, 3));
|
---|
727 | }
|
---|
728 | else {
|
---|
729 | ep->hexserial[3] = ep->emm[6];
|
---|
730 | ep->type = UNIQUE;
|
---|
731 | return (!memcmp (rdr->hexserial+2, ep->hexserial, 4));
|
---|
732 | }
|
---|
733 | case 0x82:
|
---|
734 | ep->type = GLOBAL;
|
---|
735 | return TRUE;
|
---|
736 | default:
|
---|
737 | ep->type = UNKNOWN;
|
---|
738 | return TRUE;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | void nagra2_get_emm_filter(struct s_reader * rdr, uchar *filter)
|
---|
743 | {
|
---|
744 | filter[0]=0xFF;
|
---|
745 | filter[1]=3;
|
---|
746 |
|
---|
747 |
|
---|
748 | filter[2]=GLOBAL;
|
---|
749 | filter[3]=0;
|
---|
750 |
|
---|
751 | filter[4+0] = 0x82;
|
---|
752 | filter[4+0+16] = 0xFF;
|
---|
753 |
|
---|
754 |
|
---|
755 | filter[36]=SHARED;
|
---|
756 | filter[37]=0;
|
---|
757 |
|
---|
758 | filter[38+0] = 0x83;
|
---|
759 | filter[38+1] = rdr->hexserial[4];
|
---|
760 | filter[38+2] = rdr->hexserial[3];
|
---|
761 | filter[38+3] = rdr->hexserial[2];
|
---|
762 | filter[38+4] = 0x00;
|
---|
763 | filter[38+5] = 0x10;
|
---|
764 | memset(filter[38+0+16], 0xFF, 6);
|
---|
765 |
|
---|
766 |
|
---|
767 | filter[70]=UNIQUE;
|
---|
768 | filter[71]=0;
|
---|
769 |
|
---|
770 | filter[72+0] = 0x83;
|
---|
771 | filter[72+1] = rdr->hexserial[4];
|
---|
772 | filter[72+2] = rdr->hexserial[3];
|
---|
773 | filter[72+3] = rdr->hexserial[2];
|
---|
774 | filter[72+4] = rdr->hexserial[5];
|
---|
775 | filter[72+5] = 0x00;
|
---|
776 | memset(filter[72+0+16], 0xFF, 6);
|
---|
777 |
|
---|
778 | return;
|
---|
779 | }
|
---|
780 | int nagra2_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
781 | {
|
---|
782 | def_resp;
|
---|
783 | if (!reader->is_tiger)
|
---|
784 | {
|
---|
785 | if(!do_cmd(reader, ep->emm[8],ep->emm[9]+2,0x84,0x02,ep->emm+8+2,cta_res,&cta_lr))
|
---|
786 | {
|
---|
787 | cs_debug("[nagra-reader] nagra2_do_emm failed");
|
---|
788 | return ERROR;
|
---|
789 | }
|
---|
790 | // for slow t14 nagra cards, we must do additional timeout
|
---|
791 | if (reader->is_pure_nagra==1)
|
---|
792 | {
|
---|
793 | cs_sleepms(300);
|
---|
794 | }
|
---|
795 | cs_sleepms(250);
|
---|
796 | nagra2_post_process(reader);
|
---|
797 | }
|
---|
798 | else
|
---|
799 | {
|
---|
800 | //check EMM prov id
|
---|
801 | if (memcmp(&reader->prid[0][2], ep->emm+10, 2))
|
---|
802 | return ERROR;
|
---|
803 |
|
---|
804 | // emm_data: 82 70 8E 00 00 00 00 00 D3 87 8D 11 C0 F4 B1 27 2C 3D 25 94 ...
|
---|
805 | //serial_data: A0 CA 00 00 8C D3 8A 01 00 00 00 00 C0 F4 B1 27 2C 3D 25 94 ...
|
---|
806 | unsigned char emm_trim[150];
|
---|
807 | memset(emm_trim, 0, 150);
|
---|
808 | memcpy(&emm_trim[5], ep->emm+3+5+2+2, ep->emm[9]+2);
|
---|
809 | if(!do_cmd(reader, ep->emm[8],ep->emm[9]+5,0x53,0x16, emm_trim,cta_res,&cta_lr))
|
---|
810 | {
|
---|
811 | cs_debug("[nagra-reader] nagra2_do_emm failed");
|
---|
812 | return ERROR;
|
---|
813 | }
|
---|
814 | cs_sleepms(300);
|
---|
815 | }
|
---|
816 | return OK;
|
---|
817 | }
|
---|