Changeset 1049
- Timestamp:
- 01/03/10 17:33:54 (13 years ago)
- Location:
- branches/smartreader
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/smartreader/csctapi/protocol_t1.c
r842 r1049 177 177 int ret; 178 178 bool more; 179 if (APDU_Cmd_Ins(cmd) == T1_BLOCK_S_IFS_REQ) 180 { 181 BYTE inf = APDU_Cmd_P2(cmd); 182 183 /* Create an IFS request S-Block */ 184 block = T1_Block_NewSBlock (T1_BLOCK_S_IFS_REQ, 1, &inf); 185 186 #ifdef DEBUG_PROTOCOL 187 printf ("Protocol: Sending block S(IFS request, %d)\n", inf); 188 #endif 189 /* Send IFSD request */ 190 ret = Protocol_T1_SendBlock (t1, block); 191 192 /* Delete block */ 193 T1_Block_Delete (block); 194 195 /* Receive a block */ 196 ret = Protocol_T1_ReceiveBlock (t1, &block); 197 198 if (ret == PROTOCOL_T1_OK) 199 { 200 rsp_type = T1_Block_GetType (block); 201 202 /* Positive IFS Response S-Block received */ 203 if (rsp_type == T1_BLOCK_S_IFS_RES) 204 { 205 /* Update IFSD value */ 206 inf = (*T1_Block_GetInf (block)); 207 t1->ifsd = inf; 208 #ifdef DEBUG_PROTOCOL 209 printf ("Protocol: Received block S(IFS response, %d)\n", inf); 210 #endif 211 } 212 } 213 214 return ret; 215 } 179 216 180 217 /* Calculate the number of bytes to send */ -
branches/smartreader/module-newcamd.c
r727 r1049 64 64 break; 65 65 case COMMTYPE_SERVER: 66 if( *netMsgId == 0xFFFE ) *netMsgId = 0; // ���0xFFFF ?66 if( *netMsgId == 0xFFFE ) *netMsgId = 0; // ??? 0xFFFF ? 67 67 break; 68 68 } … … 715 715 { 716 716 if (cfg->ncd_ptab.ports[client[cs_idx].port_idx].ftab.filts[0].caid != reader[au].caid[0] 717 && cfg->ncd_ptab.ports[client[cs_idx].port_idx].ftab.filts[0].caid != reader[au].ftab.filts[0].caid) 717 && cfg->ncd_ptab.ports[client[cs_idx].port_idx].ftab.filts[0].caid != reader[au].ftab.filts[0].caid) 718 718 { 719 719 cs_log("AU wont be used on this port -> disable AU"); … … 851 851 mbuf[12] = reader[au].hexserial[3]; 852 852 mbuf[13] = reader[au].hexserial[4]; 853 } 854 else if ((pufilt->caid >> 8) == 0x18) 855 { 856 mbuf[8] = 0x00; 857 mbuf[9] = 0x00; 858 mbuf[10] = reader[au].hexserial[0]; 859 mbuf[11] = reader[au].hexserial[1]; 860 mbuf[12] = reader[au].hexserial[2]; 861 mbuf[13] = reader[au].hexserial[3]; 853 862 } 854 863 else -
branches/smartreader/oscam-config.c
r1038 r1049 68 68 #endif 69 69 70 staticvoid chk_iprange(char *value, struct s_ip **base)71 { 72 //int i;70 void chk_iprange(char *value, struct s_ip **base) 71 { 72 int i = 0; 73 73 char *ptr1, *ptr2; 74 74 struct s_ip *lip, *cip; … … 89 89 for (ptr1=strtok(value, ","); ptr1; ptr1=strtok(NULL, ",")) 90 90 { 91 if (i == 0) ++i; 92 else { 93 if (!(cip=malloc(sizeof(struct s_ip)))){ 94 fprintf(stderr, "Error allocating memory (errno=%d)\n", errno); 95 exit(1); 96 } 97 lip->next = cip; 98 memset(cip, 0, sizeof(struct s_ip)); 99 } 91 100 if( (ptr2=strchr(trim(ptr1), '-')) ) 92 101 { … … 97 106 else 98 107 cip->ip[0]=cip->ip[1]=cs_inet_addr(ptr1); 108 lip = cip; 99 109 } 100 110 } -
branches/smartreader/reader-nagra.c
r821 r1049 4 4 #include <termios.h> 5 5 #include <unistd.h> 6 #ifdef OS_LINUX7 #include <linux/serial.h>8 #endif9 6 10 7 IDEA_KEY_SCHEDULE ksSession; … … 91 88 return 0; 92 89 } 93 if (is_pure_nagra==1) msg[4]+=1; 94 if (is_tiger) 90 if (is_pure_nagra==1) 95 91 { 96 msg[4]--; 97 msg[6]--; 92 msg[4]+=1; 98 93 } 99 94 if(!reader_cmd2icc(msg,msglen)) 100 95 { 101 cs_sleepms( 10);96 cs_sleepms(20); 102 97 if(cta_res[0]!=res) 103 98 { … … 128 123 cs_debug("[nagra-reader] IFS cmd: %s", cs_hexdump (1, buf, 5)); 129 124 ret = reader_cmd2api(buf, 5); 130 cs_debug("[nagra-reader] IFS response: %s", cs_hexdump (1, cta_res, cta_lr)); 131 if ((cta_lr!=5) || (ret!=OK)) 125 if (ret < 0) 132 126 { 133 127 cs_debug("[nagra-reader] setting IFS to %02x failed", size); … … 190 184 { 191 185 do_cmd(0xC8,0x02,0xB8,0x06,NULL); 192 193 /*194 [sci0] <- 21 00 08 a0 ca 00 00 02 c8 00 06 8f195 [sci0] -> 12 00 08196 [sci0] -> b8 04 19 90 7b f4 90 00197 */198 186 } 199 187 … … 213 201 memcpy(camid,reader[ridx].hexserial,4); 214 202 } 203 } 204 205 int NegotiateSessionKey_Tiger(void) 206 { 207 unsigned char rsa_modulo[120]; 208 unsigned char idea_sig[16]; 209 unsigned char vFixed[] = {0,1,2,3,0x11}; 210 unsigned char parte_fija[120]; 211 unsigned char parte_variable[88]; 212 unsigned char d1_rsa_modulo[88]; 213 unsigned char rand[88]; 214 unsigned char d2_data[88]; 215 unsigned char sign1[8]; 216 unsigned char sessi1[8]; 217 unsigned char sessi2[8]; 218 unsigned char tmp[104]; 219 unsigned char tmp1[8]; 220 221 if(!do_cmd(0xd1,0x02,0x51,0xd2,NULL)) 222 { 223 cs_debug("[nagra-reader] CMD$D1 failed"); 224 return 0; 225 } 226 227 ReverseMem(rsa_modulo, 120); 228 ReverseMem(&cta_res[90], 120); 229 230 cs_debug("[nagra-reader] crypted parte_fija: %s", cs_hexdump (0, &cta_res[90], 32)); 231 cs_debug("[nagra-reader] crypted parte_fija: %s", cs_hexdump (0, &cta_res[122], 32)); 232 cs_debug("[nagra-reader] crypted parte_fija: %s", cs_hexdump (0, &cta_res[154], 32)); 233 cs_debug("[nagra-reader] crypted parte_fija: %s", cs_hexdump (0, &cta_res[186], 24)); 234 235 BN_CTX *ctx = BN_CTX_new(); 236 BIGNUM *bnN = BN_CTX_get(ctx); 237 BIGNUM *bnE = BN_CTX_get(ctx); 238 BIGNUM *bnCT = BN_CTX_get(ctx); 239 BIGNUM *bnPT = BN_CTX_get(ctx); 240 BN_bin2bn(rsa_modulo, 120, bnN); 241 BN_bin2bn(vFixed+4, 1, bnE); 242 BN_bin2bn(&cta_res[90], 120, bnCT); 243 BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx); 244 memset(parte_fija, 0, 120); 245 BN_bn2bin(bnPT, parte_fija + (120-BN_num_bytes(bnPT))); 246 BN_CTX_end(ctx); 247 BN_CTX_free (ctx); 248 249 ReverseMem(parte_fija, 120); 250 cs_debug("[nagra-reader] decrypted parte_fija: %s", cs_hexdump (0, parte_fija, 32)); 251 cs_debug("[nagra-reader] decrypted parte_fija: %s", cs_hexdump (0, &parte_fija[32], 32)); 252 cs_debug("[nagra-reader] decrypted parte_fija: %s", cs_hexdump (0, &parte_fija[64], 32)); 253 cs_debug("[nagra-reader] decrypted parte_fija: %s", cs_hexdump (0, &parte_fija[96], 24)); 254 255 cs_debug("[nagra-reader] ---------- SIG CHECK ---------------------"); 256 memset(tmp,0, 104); 257 memcpy(tmp+4, parte_fija+11, 100); //104 258 Signature(sign1, idea_sig, tmp, 112); 259 cs_debug("[nagra-reader] foo: %s", cs_hexdump (0, tmp, 32)); 260 cs_debug("[nagra-reader] foo: %s", cs_hexdump (0, &tmp[32], 32)); 261 cs_debug("[nagra-reader] foo: %s", cs_hexdump (0, &tmp[64], 32)); 262 cs_debug("[nagra-reader] foo: %s", cs_hexdump (0, &tmp[96], 8)); 263 cs_debug("[nagra-reader] sign1: %s", cs_hexdump (0, sign1, 8)); 264 cs_debug("[nagra-reader] sign2: %s", cs_hexdump (0, parte_fija+111, 8)); 265 if (!memcmp (parte_fija+111, sign1, 8)==0) 266 { 267 cs_debug("[nagra-reader] signature check nok"); 268 return 0; 269 } 270 cs_debug("[nagra-reader] ------------------------------------------"); 271 cs_debug("[nagra-reader] signature check ok"); 272 cs_debug("[nagra-reader] part2 of algo not implemented yet."); 273 274 /* 275 A lot of todo here. 276 Second part of algo removed due it fails 277 */ 278 279 if(!do_cmd(0xd2,0x5a,0x52,0x03, d2_data)) 280 { 281 cs_debug("[nagra-reader] CMD$D2 failed"); 282 return 0; 283 } 284 if (cta_res+2 == 0x00) 285 { 286 memcpy(sessi,sessi1,8); memcpy(sessi+8,sessi2,8); 287 cs_ri_log("[nagra-reader] session key: %s", cs_hexdump(1, sessi, 16)); 288 return 1; 289 } 290 cs_ri_log("Negotiate sessionkey was not successfull! Please check rsa key and boxkey"); 291 return 0; 292 215 293 } 216 294 … … 232 310 if (is_tiger) 233 311 { 234 unsigned char d1[] = {0xd2}; 235 if(!do_cmd(0xd1,0x03,0x51,0x42,d1)) 236 { 237 cs_debug("[nagra-reader] CMD$D1 failed"); 312 if (!NegotiateSessionKey_Tiger()) 313 { 314 cs_debug("[nagra-reader] NegotiateSessionKey_Tiger failed"); 238 315 return 0; 239 316 } 240 } 241 else 242 { 243 if(!do_cmd(0x2a,0x02,0xaa,0x42,NULL)) 244 { 245 cs_debug("[nagra-reader] CMD$2A failed"); 246 return 0; 247 } 248 } 249 250 // RSA decrypt of cmd$2a data, result is stored in "negot" 251 ReverseMem(cta_res+2, 64); 252 //cs_debug("[nagra-reader] plainDT08RSA: %s", cs_hexdump (1, plainDT08RSA, 32)); 253 //cs_debug("[nagra-reader] plainDT08RSA: %s", cs_hexdump (1, &plainDT08RSA[32], 32)); 254 unsigned char vFixed[] = {0,1,2,3}; 255 BN_CTX *ctx = BN_CTX_new(); 256 BIGNUM *bnN = BN_CTX_get(ctx); 257 BIGNUM *bnE = BN_CTX_get(ctx); 258 BIGNUM *bnCT = BN_CTX_get(ctx); 259 BIGNUM *bnPT = BN_CTX_get(ctx); 260 BN_bin2bn(plainDT08RSA, 64, bnN); 261 BN_bin2bn(vFixed+3, 1, bnE); 262 BN_bin2bn(cta_res+2, 64, bnCT); 263 BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx); 264 memset(negot, 0, 64); 265 BN_bn2bin(bnPT, negot + (64-BN_num_bytes(bnPT))); 266 //cs_debug("[nagra-reader] DT08 decrypted $2a data: %s", cs_hexdump (1, negot, 32)); 267 //cs_debug("[nagra-reader] DT08 decrypted $2a data: %s", cs_hexdump (1, &negot[32], 32)); 268 269 memcpy(tmp, negot, 64); 270 ReverseMem(tmp, 64); 317 return 1; 318 } 319 if(!do_cmd(0x2a,0x02,0xaa,0x42,NULL)) 320 { 321 cs_debug("[nagra-reader] CMD$2A failed"); 322 return 0; 323 } 324 325 // RSA decrypt of cmd$2a data, result is stored in "negot" 326 ReverseMem(cta_res+2, 64); 327 //cs_debug("[nagra-reader] plainDT08RSA: %s", cs_hexdump (1, plainDT08RSA, 32)); 328 //cs_debug("[nagra-reader] plainDT08RSA: %s", cs_hexdump (1, &plainDT08RSA[32], 32)); 329 unsigned char vFixed[] = {0,1,2,3}; 330 BN_CTX *ctx = BN_CTX_new(); 331 BIGNUM *bnN = BN_CTX_get(ctx); 332 BIGNUM *bnE = BN_CTX_get(ctx); 333 BIGNUM *bnCT = BN_CTX_get(ctx); 334 BIGNUM *bnPT = BN_CTX_get(ctx); 335 BN_bin2bn(plainDT08RSA, 64, bnN); 336 BN_bin2bn(vFixed+3, 1, bnE); 337 BN_bin2bn(cta_res+2, 64, bnCT); 338 BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx); 339 memset(negot, 0, 64); 340 BN_bn2bin(bnPT, negot + (64-BN_num_bytes(bnPT))); 341 //cs_debug("[nagra-reader] DT08 decrypted $2a data: %s", cs_hexdump (1, negot, 32)); 342 //cs_debug("[nagra-reader] DT08 decrypted $2a data: %s", cs_hexdump (1, &negot[32], 32)); 343 344 memcpy(tmp, negot, 64); 345 ReverseMem(tmp, 64); 346 347 // build sessionkey 348 // 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 349 memcpy(idea1, signature, 8); 350 memcpy(idea1+8, reader[ridx].hexserial, 4); 351 idea1[12] = ~reader[ridx].hexserial[0]; idea1[13] = ~reader[ridx].hexserial[1]; idea1[14] = ~reader[ridx].hexserial[2]; idea1[15] = ~reader[ridx].hexserial[3]; 271 352 272 // build sessionkey 273 // 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 274 memcpy(idea1, signature, 8); 275 memcpy(idea1+8, reader[ridx].hexserial, 4); 276 idea1[12] = ~reader[ridx].hexserial[0]; idea1[13] = ~reader[ridx].hexserial[1]; idea1[14] = ~reader[ridx].hexserial[2]; idea1[15] = ~reader[ridx].hexserial[3]; 353 Signature(sign1, idea1, tmp, 32); 354 memcpy(idea2,sign1,8); memcpy(idea2+8,sign1,8); 355 Signature(sign2, idea2, tmp, 32); 356 memcpy(sessi,sign1,8); memcpy(sessi+8,sign2,8); 277 357 278 Signature(sign1, idea1, tmp, 32); 279 memcpy(idea2,sign1,8); memcpy(idea2+8,sign1,8); 280 Signature(sign2, idea2, tmp, 32); 281 memcpy(sessi,sign1,8); memcpy(sessi+8,sign2,8); 358 // prepare cmd$2b data 359 BN_bin2bn(negot, 64, bnCT); 360 BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx); 361 memset(cmd2b+10, 0, 64); 362 BN_bn2bin(bnPT, cmd2b+10 + (64-BN_num_bytes(bnPT))); 363 BN_CTX_end(ctx); 364 BN_CTX_free (ctx); 365 ReverseMem(cmd2b+10, 64); 282 366 283 // prepare cmd$2b data 284 BN_bin2bn(negot, 64, bnCT); 285 BN_mod_exp(bnPT, bnCT, bnE, bnN, ctx); 286 memset(cmd2b+10, 0, 64); 287 BN_bn2bin(bnPT, cmd2b+10 + (64-BN_num_bytes(bnPT))); 288 BN_CTX_end(ctx); 289 BN_CTX_free (ctx); 290 ReverseMem(cmd2b+10, 64); 291 292 IDEA_KEY_SCHEDULE ks; 293 idea_set_encrypt_key(sessi,&ks); 294 idea_set_decrypt_key(&ks,&ksSession); 295 296 if (!is_tiger) 297 { 298 if(!do_cmd(0x2b,0x42,0xab,0x02, cmd2b+10)) 299 { 300 cs_debug("[nagra-reader] CMD$2B failed"); 301 return 0; 302 } 303 } 304 else 305 { 306 if(!do_cmd(0xd2,0x42,0x52,0x03, cmd2b+10)) 307 { 308 cs_debug("[nagra-reader] CMD$D2 failed"); 309 return 0; 310 } 311 } 312 367 IDEA_KEY_SCHEDULE ks; 368 idea_set_encrypt_key(sessi,&ks); 369 idea_set_decrypt_key(&ks,&ksSession); 370 371 if(!do_cmd(0x2b,0x42,0xab,0x02, cmd2b+10)) 372 { 373 cs_debug("[nagra-reader] CMD$2B failed"); 374 return 0; 375 } 376 313 377 cs_debug("[nagra-reader] session key: %s", cs_hexdump(1, sessi, 16)); 314 378 315 if (!is_tiger) 316 { 317 if (!CamStateRequest()) 318 { 319 cs_debug("[nagra-reader] CamStateRequest failed"); 320 return 0; 321 } 322 if SENDDATETIME 323 { 324 DateTimeCMD(); 325 } 326 if RENEW_SESSIONKEY 327 { 328 cs_ri_log("Negotiate sessionkey was not successfull! Please check rsa key and boxkey"); 329 return 0; 330 } 331 } 379 if (!CamStateRequest()) 380 { 381 cs_debug("[nagra-reader] CamStateRequest failed"); 382 return 0; 383 } 384 if SENDDATETIME 385 { 386 DateTimeCMD(); 387 } 388 if RENEW_SESSIONKEY 389 { 390 cs_ri_log("Negotiate sessionkey was not successfull! Please check rsa key and boxkey"); 391 return 0; 392 } 393 332 394 return 1; 333 395 } … … 502 564 if (memcmp(atr+11, "DNASP", 5)==0) 503 565 { 504 //if(SetIFS(0xFE) != 1) return 0;566 if(SetIFS(0xFE) != 1) return 0; 505 567 cs_debug("[nagra-reader] detect native nagra card T1 protocol"); 506 568 memcpy(rom,atr+11,15); … … 508 570 else if (memcmp(atr+11, "TIGER", 5)==0) 509 571 { 510 //if(SetIFS(0xFE) != 1) return 0;572 if(SetIFS(0xFE) != 1) return 0; 511 573 cs_debug("[nagra-reader] detect nagra tiger card"); 512 574 memcpy(rom,atr+11,15); … … 594 656 void nagra2_post_process(void) 595 657 { 596 //Todo: Do not block!//597 658 CamStateRequest(); 598 659 cs_sleepms(10); … … 616 677 617 678 } 618 cs_sleepms( 5);679 cs_sleepms(15); 619 680 while(!CamStateRequest() && retry < 5) 620 681 { … … 622 683 cs_sleepms(15); 623 684 } 624 cs_sleepms( 5);685 cs_sleepms(10); 625 686 if (HAS_CW && do_cmd(0x1C,0x02,0x9C,0x36,NULL)) 626 687 { … … 644 705 if(!do_cmd(0xd3,er->ecm[4]+2,0x53,0x16, er->ecm+3+2)) 645 706 { 646 //53 14 01 00 00 00 647 //94 8c 3e f7 49 a8 c7 23 //cw1? 648 //9a 67 e3 e1 d4 c1 c7 83 //cw0? 649 //90 0 650 unsigned char v[8]; 651 memset(v,0,sizeof(v)); 652 idea_cbc_encrypt(&cta_res[14],er->cw,8,&ksSession,v,IDEA_DECRYPT); 653 memset(v,0,sizeof(v)); 654 idea_cbc_encrypt(&cta_res[6],er->cw+8,8,&ksSession,v,IDEA_DECRYPT); 655 return (1); 707 if(cta_res[2] == 0x01) 708 { 709 unsigned char v[8]; 710 memset(v,0,sizeof(v)); 711 idea_cbc_encrypt(&cta_res[14],er->cw,8,&ksSession,v,IDEA_DECRYPT); 712 memset(v,0,sizeof(v)); 713 idea_cbc_encrypt(&cta_res[6],er->cw+8,8,&ksSession,v,IDEA_DECRYPT); 714 return (1); 715 } 716 cs_debug("[nagra-reader] can't decode ecm"); 717 return (0); 656 718 } 657 719 }
Note:
See TracChangeset
for help on using the changeset viewer.