Z13 Process
unknown
c_cpp
a month ago
8.3 kB
6
Indexable
Never
uint process_Z13() { TLV_STRUCT listRkey[TLV_LIST_LENGTH]; int iRet; uchar Key_Name[10+1] = { 0 }; uchar RsaIndex; uchar FindKey = FALSE, FindKey2 = FALSE; uchar Data[2+1]={0}; uchar KeyIndex, KeyType=0;// , Padding; ST_RSA_KEY RsakeyOut; uchar SendBuf[20]; int offset=0; ST_KCV_INFO kcv_info; uchar blockCIPHERbcd[256]={0}, blockCIPHERbcd2[256]={0}; uchar DataOut[16]={0}; uchar LLaveCorrecta = TRUE; uchar Continue = TRUE; uchar LlaveOpenpay = FALSE; uchar FixedKey = FALSE; //Obtiene la lista de tags TLV iRet = getListTLV(APP_STATUS.command_data, APP_STATUS.command_data_len, listRkey); if (iRet != OK_RESULT) { sendError(_Z13, ERROR_DATAFORMAT, COMRESP_ERROR_DATAFORMAT_B1, COMRESP_ERROR_DATAFORMAT_B2, NULL); return ERROR_RESULT; } //valida el comando if (validateCommandZ13 (listRkey) == FALSE) { //aborta el proceso uchar responseBytes[2]; APP_STATUS.current_error_code = ERROR_DATAFORMAT; //validar el codigo de error actual getResponseBytes(APP_STATUS.current_error_code, responseBytes); sendError(_Z13, ERROR_DATAFORMAT, responseBytes[0], responseBytes[1], NULL); // return ERRORTRANS_RESULT; } //Key Name if(listRkey[0].len==10) { memcpy(&Key_Name, listRkey[0].value, listRkey[0].len); } else //if( listRkey[0].len!=0 ) { paintMessageDelayMs("Long. KeyName Incorrecta "); sendError(_Z13, ERROR_DATAFORMAT, COMRESP_ERROR_DATAFORMAT_B1, COMRESP_ERROR_DATAFORMAT_B2, NULL); return ERROR_RESULT; } //check for RSA index if (memcmp(Key_Name, LLAVE_001, 10) == 0 || memcmp(Key_Name, LLAVE_002, 10) == 0 || memcmp(Key_Name, LLAVE_003, 10) == 0 || memcmp(Key_Name, LLAVE_004, 10) == 0) { if (memcmp(Key_Name, LLAVE_001, 10) == 0) RsaIndex = IDX_LLAVE_001; else if (memcmp(Key_Name, LLAVE_002, 10) == 0) { RsaIndex = IDX_LLAVE_002; FixedKey = TRUE; } else if (memcmp(Key_Name, LLAVE_003, 10) == 0) { RsaIndex = IDX_LLAVE_003; FixedKey = TRUE; } else if (memcmp(Key_Name, LLAVE_004, 10) == 0) RsaIndex = IDX_LLAVE_004; memset(&RsakeyOut, 0, sizeof(ST_RSA_KEY)); iRet = PedReadRsaKey(RsaIndex, &RsakeyOut); if (PED_RET_OK == iRet || (PED_RET_ERR_KEY_TYPE_ERR == iRet && IDX_LLAVE_004 == RsaIndex)) { if (RsaIndex != IDX_LLAVE_002 && RsaIndex != IDX_LLAVE_003) { LlaveOpenpay = TRUE; } FindKey = TRUE; } } else { RsaIndex = 5; Continue = TRUE; FindKey = FALSE; do { memset(&RsakeyOut, 0, sizeof(ST_RSA_KEY)); iRet = PedReadRsaKey(RsaIndex, &RsakeyOut); //ScrCls();Lcdprintf("%d %s", RsaIndex, RsakeyOut.aucKeyInfo);getkey(); if (PED_RET_OK != iRet) { RsaIndex++; } else if (memcmp(RsakeyOut.aucKeyInfo, Key_Name, 10) == 0) { //ScrCls();Lcdprintf("Encontrado %s", Key_Name);getkey(); Continue = FALSE; FindKey = TRUE; } else RsaIndex++; if (RsaIndex > MAX_LLAVES_RSA) { //ScrCls();Lcdprintf("No Encontrado %s", Key_Name);getkey(); Continue = FALSE; } } while (Continue); } if(FindKey==FALSE) LLaveCorrecta = FALSE; if(memcmp(Key_Name, LLAVE_PUBLICA_OP, 10)==0) { LlaveOpenpay = TRUE; } if(LlaveOpenpay==FALSE) { if (FixedKey == TRUE) { KeyIndex = RsaIndex; KeyType = PED_TIK; } else { //key Index memset(Data, 0, sizeof(Data)); memcpy(Data, &RsakeyOut.aucKeyInfo[10], 2); KeyIndex = atoi(Data); //KeyType memset(Data, 0, sizeof(Data)); PubAsc2Bcd(&RsakeyOut.aucKeyInfo[10 + 2], 2, Data); KeyType = Data[0]; if (KeyType == 0x10) KeyType = PED_TIK; //Padding //memset(Data, 0, sizeof(Data)); //memcpy(Data, &RsakeyOut.aucKeyInfo[10+2+2], 2); //Padding = atoi(Data); if (KeyIndex == 0) KeyIndex = RsaIndex; if (KeyType == 0) KeyType = PED_TIK; } } //Hash RSA if(LlaveOpenpay==FALSE) { if(FindKey) { char buffer_in[256], buffer_out[256]; char pucKeyInfoOut[128] = { 0 }; memset(buffer_in, '0', sizeof(buffer_in)); memset(buffer_out, 0, sizeof(buffer_out)); iRet = PedRsaRecover(RsaIndex, (uchar*)buffer_in, (uchar*)buffer_out, (uchar*)pucKeyInfoOut); //sprintf(msg, "SHA1 ret %d Mod len %d", iRet, RsakeyOut.iModulusLen/8);paintMessage(msg);getkey(); if (iRet != PED_RET_OK) { LLaveCorrecta = FALSE; } else { LLaveCorrecta = TRUE; memcpy(blockCIPHERbcd, buffer_out, 4); } //leer KCV de llave if(KeyType!=PED_TAESK) { memset(&kcv_info, 0, sizeof(ST_KCV_INFO)); kcv_info.iCheckMode=0x00; if(KeyType==PED_TIK) kcv_info.aucCheckBuf[0]=0x04; else kcv_info.aucCheckBuf[0]=0x08; iRet= PedGetKcv(KeyType, KeyIndex, &kcv_info); if (iRet != PED_RET_OK) { LLaveCorrecta = FALSE; } } else if(KeyType==PED_TAESK) { uchar Data[16]={0}; iRet = PedCalcAes(KeyIndex,NULL,Data,16,DataOut,PED_ENCRYPT); if (iRet != PED_RET_OK) { LLaveCorrecta = FALSE; } } } else { char buffer_in[256], buffer_out[256]; char pucKeyInfoOut[128] = { 0 }; memset(buffer_in, '0', sizeof(buffer_in)); memset(buffer_out, 0, sizeof(buffer_out)); iRet = PedRsaRecover(RsaIndex, (uchar*)buffer_in, (uchar*)buffer_out, (uchar*)pucKeyInfoOut); if (iRet != PED_RET_OK) { LLaveCorrecta = FALSE; } } } else { //char msg[50]={0}; char buffer_in[256], buffer_out[256]; char pucKeyInfoOut[128] = {0}; if(FindKey) { if (memcmp(Key_Name, LLAVE_001, 10) == 0) RsaIndex = IDX_LLAVE_001; /*else if (memcmp(Key_Name, LLAVE_002, 10) == 0) RsaIndex = IDX_LLAVE_002; else if (memcmp(Key_Name, LLAVE_003, 10) == 0) RsaIndex = IDX_LLAVE_003;*/ else if (memcmp(Key_Name, LLAVE_004, 10) == 0) RsaIndex = IDX_LLAVE_004; memset(buffer_in, '0', sizeof(buffer_in)); memset(buffer_out, 0, sizeof(buffer_out)); iRet = PedRsaRecover(RsaIndex, (uchar*)buffer_in, (uchar*)buffer_out, (uchar*)pucKeyInfoOut); if (iRet == PED_RET_OK) { memcpy(blockCIPHERbcd, buffer_out, 4); } } if (RsaIndex == IDX_LLAVE_001) { memset(buffer_in, '0', sizeof(buffer_in)); memset(buffer_out, 0, sizeof(buffer_out)); RsaIndex = IDX_LLAVE_004; iRet = PedRsaRecover(RsaIndex, (uchar*)buffer_in, (uchar*)buffer_out, (uchar*)pucKeyInfoOut); if (iRet != PED_RET_OK) { //ScrCls();Lcdprintf("SHA2 %d", iRet);getkey(); LLaveCorrecta = FALSE; } else //if(memcmp(pucKeyInfoOut, LLAVE_PRIVADA_OP, 10)==0) { FindKey2 = TRUE; memcpy(blockCIPHERbcd2, buffer_out, 4); //sprintf(msg, "PedRsaRecover ret %d %s", iRet, pucKeyInfoOut);paintMessage(msg);getkey(); } } } //envia respuesta del Z13 cleanBuff(SendBuf, sizeof(SendBuf)); SendBuf[offset++] = STX; SendBuf[offset++] = _Z1XB1; SendBuf[offset++] = _Z1XB2; SendBuf[offset++] = _Z13B3; if(LLaveCorrecta==TRUE) { SendBuf[offset++] = COMRESP_OK_B1; SendBuf[offset++] = COMRESP_OK_B1; } else { SendBuf[offset++] = COMRESP_ERROR_KEYNOTEXIST_B1; SendBuf[offset++] = COMRESP_ERROR_KEYNOTEXIST_B2; } if(FindKey) { makeTLV(0xC1, 4, blockCIPHERbcd, SendBuf + offset); offset+=6; } else { SendBuf[offset++] = 0xC1; SendBuf[offset++] = 0x00; } if(LlaveOpenpay==FALSE && LLaveCorrecta==TRUE) { if(KeyType!=PED_TAESK) { makeTLV(0xC1, 4, kcv_info.aucCheckBuf, SendBuf + offset); } else { makeTLV(0xC1, 4, DataOut, SendBuf + offset); } offset+=6; } else if(LlaveOpenpay==TRUE && FindKey2==TRUE) { makeTLV(0xC1, 4, blockCIPHERbcd2, SendBuf + offset); offset+=6; } else { SendBuf[offset++] = 0xC1; SendBuf[offset++] = 0x00; } SendBuf[offset] = ETX; PubCalcLRC(SendBuf + sizeof(uchar), offset, &LRC); SendBuf[++offset] = LRC; offset++; // if(LLaveCorrecta) { paintMessageDelayMs("Llave validada"); } else { APP_STATUS.current_error_code = ERROR_CRYPTO; paintMessage(KEY_TEST_MSG); DelayMs(200); } ScrCls(); APP_STATUS.current_process = PROCESS_NONE; // sendMessage( SendBuf, offset); // return OK_RESULT; }
Leave a Comment