📄 sci_prot.c
字号:
} /* WWT timeout OK if we successfully read the procedure byte */ if ((rc == SCI_ERROR_OK) || (rc == SCI_ERROR_WWT_TIMEOUT)) { //rc = SCI_ERROR_OK; if(bytes_rec > 0) { if (end == 1) { p_end_sequence[1] = current; rc = SCI_ERROR_OK; done = 1; } else if (((current >= 0x61) && (current <= 0x6F)) || (current >= 0x90) && (current <= 0x9F)) { /* this is the end sequence, so save SW1 and flag the end sequence */ p_end_sequence[0] = current; end = 1; } else if ((current == ins) || (current == (ins ^ 0x01))) { /* if the ACK is equal to the instruction, send/receive the command data */ if (direction == 0) { //if (write(fd[sc_id], p_body, bytes_left) <= 0) if ((rc = write(fd[sc_id], p_body, bytes_left)) <= 0) { //rc = errno; bytes_left = 0; done = 1; } else { rc = SCI_ERROR_OK; } } } else if ((current == (ins ^ 0xFF)) || (current == (ins ^ 0xFE))) { /* send/receive only one byte */ if (direction == 0) { if (bytes_left > 0) { //if (write(sc_id, p_body, 1) > 0) if ((rc = write(sc_id, p_body, 1)) > 0) { rc = SCI_ERROR_OK; } /*else { rc = errno; }*/ p_body++; bytes_left--; } else { rc = SCI_ERROR_FAIL; done = 1; } } else { if ((bytes_rec = read(fd[sc_id], p_body, 1)) > 0) { rc = SCI_ERROR_OK; if (bytes_rec == 1) { bytes_left--; p_body++; } else { rc = SCI_ERROR_FAIL; done = 1; } } else { //rc = errno; rc = bytes_rec; } } } else { if (current != 0x60) { rc = SCI_ERROR_FAIL; done = 1; } } } else { if(rc == SCI_ERROR_OK) { rc = SCI_ERROR_FAIL; } done = 1; } } } } return (rc);}/****************************************************************************** Name sc_t1_command**** Purpose: Perform a protocol T=1 command transaction.**** Parameters: sc_id: zero-based Smart Card identifier** p_capdu: (input) pointer to the command APDU buffer** p_rapdu: (output) pointer to the response APDU buffer** p_length:(input) pointer the length of the command APDU** (output) pointer the length of the response APDU**** Returns: SCI_ERROR_OK: if successful** other error code if a failure occurs****************************************************************************/static SCI_ERROR sc_t1_command(unsigned long sc_id, unsigned char *p_capdu, unsigned char *p_rapdu, unsigned long *p_length){ SCI_ERROR rc = SCI_ERROR_OK; SCI_PARAMETERS sci_parameters; unsigned long done = 0; unsigned char loop = 0; BLOCKS d_block_type = I; BLOCKS c_block_type = I; unsigned long capdu_length = 0; unsigned long rapdu_length = 0; unsigned long c_block_length = 0; unsigned char c_block[SC_MAX_T1_BLOCK_SIZE]; unsigned char d_block[SC_MAX_T1_BLOCK_SIZE]; unsigned char *p_data; unsigned char retry = 0; unsigned char resynch = 0; unsigned char resend = 0; //unsigned long i_block_size = 0; //unsigned char *i_block; unsigned long time = 0; p_data = p_capdu; capdu_length = *p_length; loop = 1; done = 0; /* this code does not use NAD byte */ c_block[0] = 0; /* first send IFSD request, if IFSD is not maximum */ if (sc_cb[sc_id].IFSD != sc_cb[sc_id].proposed_IFSD) { d_block_type = S; d_block[1] = S_IFS_REQUEST; d_block[2] = 1; d_block[3] = sc_cb[sc_id].proposed_IFSD; } while ((done == 0) && (rc == SCI_ERROR_OK)) { /* copy NAD */ d_block[0] = c_block[0]; /* create next block to send */ if (d_block_type == I) { if (resend == 0) { /* I-block */ /* set current state of N(S) in PCB byte */ if ((sc_cb[sc_id].NS) == 0) { d_block[1] = 0; } else { d_block[1] = I_NS; } /* toggle N(S) value in PCB byte for next I-block */ sc_cb[sc_id].NS = (sc_cb[sc_id].NS + 1) % 2; if (capdu_length <= sc_cb[sc_id].sc_parameters.IFSC) { /* no chaining required, send only 1 block */ /* set LEN byte */ d_block[2] = (unsigned char) capdu_length; capdu_length = 0; } else { /* set LEN byte */ d_block[2] = sc_cb[sc_id].sc_parameters.IFSC; capdu_length -= sc_cb[sc_id].sc_parameters.IFSC; /* set more data bit in PCB byte */ d_block[1] |= I_M; } /* copy INF field if it exists */ if (d_block[2] > 0) { memcpy((void *) (d_block + 3), (const void *) p_data, (size_t) d_block[2]); //i_block = p_data; p_data += d_block[2]; } //i_block_size = (d_block[2] + 3); } /*else { memcpy((void *) d_block, (const void *) i_block, (size_t) i_block_size); resend = 0; }*/ } /* write the block */ //if (write(fd[sc_id], d_block, (d_block[2] + 3)) > 0) if ((rc = write(fd[sc_id], d_block, (d_block[2] + 3))) > 0) { rc = SCI_ERROR_OK; /* read the response block */ while (loop > 0) { /* this may have to loop on read if a WTX request is made */ if ((c_block_length = read(fd[sc_id], c_block, SC_MAX_T1_BLOCK_SIZE)) > 0) { rc = SCI_ERROR_OK; } else { rc = c_block_length; } if (rc != SCI_ERROR_BWT_TIMEOUT) { loop = 0; } else { loop --; } } loop = 1; if (rc == SCI_ERROR_OK) { /* the block was successfully read */ /* determine block type */ if (((c_block[1]) & 0x80) == 0x80) { if (((c_block[1]) & 0x40) == 0x40) { c_block_type = S; } else { c_block_type = R; } } else { c_block_type = I; } switch (c_block_type) { case I: /* copy INF data to RAPDU */ memcpy((void *) (p_rapdu + rapdu_length), (const void *) (c_block + 3), (size_t) c_block[2]); rapdu_length += (c_block[2]); /* check more data bit */ if (((c_block[1]) & I_M) == I_M) { /* next block to send is an R-block */ d_block_type = R; d_block[2] = 0; /* N(R) is next expected N(S) */ if (((c_block[1]) & I_NS) == I_NS) { d_block[1] = 0x80; } else { d_block[1] = (0x80 | R_NR); } } else { done = 1; } break; case R: if ((((c_block[1]) & R_NR) >> 4) != sc_cb[sc_id].NS) { if(resend < 2) { resend ++; d_block_type = I; } else { resend = 0; resynch = 1; rc = SCI_ERROR_FAIL; } } else { /* if not chaining, an error h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -