Transferred from: Http://bbs.ednchina.com/BLOG_ARTICLE_172027.HTM
3. Terminal Transport Layer
This section describes the mechanism of the command and response APDU between the terminal and the IC card. APDU is a command or response message. Since both command and response messages can contain data, the TTL should be able to handle the 4 command formats defined by the application layer. The composition of C-APDU and R-APDU is described in the application layer.
Tal transmits C-apdu to TTL. Before it is sent to the IC card, it should be transformed into a form approved by the transport Protocol. After the IC card finishes processing the command, the data (if any) and the status code are sent back to the TTL in R-APDU format.
3.1 t= "0 protocol" APDU transmission
This section describes how C-APDU and R-APDU are mapped, the data exchange mechanism between TTL and IC cards, and how to retrieve the IC card data using the Get Response command in command case 2 or 4.
(1) Mapping mode and data exchange of C-APDU and R-APDU
How to map C-APDU to the t= "0" command header depends on the condition of the command. How to map the IC card loopback data (if present) and the status code to the R-APDU depends on the length of the loopback data.
The process bytes returned by the IC card SW1 sw2= ' 61xx ' and SW1 sw2= ' 6Cxx ' are used to control the exchange of data between the IC card and the TTL and it will not be sent back to TAL. Procedure byte SW1 sw2= ' 61xx ' or SW1 sw2= ' 6Cxx ' denotes life
The processing in the IC card is not completed.
If the IC card return to the TTL status code is SW1 sw2= ' 9000 ', indicating that the processing of the command has been completed properly. Any other status code indicates that the IC card has interrupted the processing of the command, and the reason for processing failure is indicated in the status code. When TTL receives any status code from an IC card (but does not include process bytes ' 61xx ' and ' 6Cxx '), it ends the processing of the command, whether it is a normal, warning, or error condition.
The following describes a method of constructing an IC card loopback data and status byte into the R-APDU format, only for the IC card has successfully completed the command processing or all data (if present) in the process byte ' 61xx ' and ' 6Cxx ' control has been returned by the IC card. The detailed use of ins, ins, and ' 60 ' procedure bytes is not described here.
A. Situation 1
The C-APDU header maps to the first four bytes of the t=0 command header, and the P3 of the t=0 command header is set to ' 00 '.
The exchange process is as follows:
* TTL sends t=0 command head to IC card;
* IC card Loopback status code to TTL.
Once the command has been processed, the status code from the IC card to the TTL loopback is mapped as-is to the R-APDU required tail field.
Note: IC card should be analyzed t=0 command header, determine whether the command condition 1 or command condition 2 in the request maximum length of response data.
B. Situation 2
The C-APDU header is mapped to the first four bytes of the t= "0" command header, and the length byte ' Le ' is mapped from the condition body of the C-APDU to the P3 of the t=0 command header.
The exchange process is as follows:
* TTL sends t=0 command head to IC card.
* Under the control of the process byte, the IC card sends back the data and the status to the TTL (only returns the state when it is not handled properly).
After the command is processed, the data (if present) and status that the IC card sends back to the TTL is mapped to R-APDU as follows:
(i) If LE≥LICC, the loopback data is mapped to the R-TPDU condition body, the returned state is not changed to be mapped to the R-APDU required tail field.
(ii) If LE<LICC, the former Le byte of the loopback data is mapped to the R-TPDU condition body, and the returned state is not changed to be mapped to the R-APDU required tail field.
C. Situation 3
The C-APDU header is mapped to the first four bytes of the t=0 command header, and the length byte ' Lc ' of the C-APDU condition body is mapped to the P3 of the t=0 command header.
The exchange process is as follows:
* TTL sends t=0 command head to IC card.
* If the IC card sends back a process byte instead of a status code, then under the control of this process byte, the TTL transmits some data of the C-APDU condition body to the IC card. If the IC card loopback status Code SW1 SW2,TTL will interrupt the command processing process.
* If the processing process is not interrupted at step 2, the IC card returns the status after the command processing has ended.
The status code that the IC card sends back to the TTL after the command is processed, or the status code returned by the IC card that causes the TTL interrupt command to be processed, is mapped to R-APDU without change.
D. Situation 4
The C-APDU header is mapped to the first four bytes of the t=0 command header, and the length byte ' Lc ' of the C-APDU condition body is mapped to the P3 of the t=0 command header.
The exchange process is as follows:
* TTL sends t=0 command header to IC card.
* If the IC card sends a process byte instead of a status code, the TTL gives the IC card partial data for the C-APDU condition body under this process byte control. If the IC card loopback status Code SW1 SW2,TTL will interrupt the command processing process.
* If the processing process is not interrupted in step 2, the IC card should send back the process byte ' 61xx ' to the TTL, request TTL issue get response command to retrieve data from the IC card. In this phase of command processing, the IC card should not be loopback status code SW1 sw2= ' 9000 '. The TTL should send the get response command to the IC card to retrieve the referenced data, which is less than or equal to the IC card loopback process byte ' xx ' byte value in bytes ' 61xx specified as the length byte of the Get Response command. This allows the GET response command to handle the same as the command for Case 2. In the process byte control, the IC card to the TTL loopback data and Status code (or in the case of abnormal processing, only the status code).
After the command is processed, the IC card returns the data to the TTL (if present) and the status code, or only the status code that causes the TTL termination command to process, and maps to R-APDU according to the following rules:
(i) If LE≥LICC, the loopback data is mapped to the R-TPDU condition body, the returned state is not changed to be mapped to the R-APDU required tail field.
(ii) If LE<LICC, the former Le bytes of the loopback data are mapped to the R-TPDU condition body, the returned state is not changed to be mapped to the R-APDU required tail field.
(2) Use of process bytes ' 61xx ' and ' 6Cxx '
The process of the return of the IC card to the TTL bytes ' 61xx ' and ' 6Cxx ' indicates how the TTL retrieves the requested data for the command currently being processed. Under the T=0 protocol, these process bytes are used only in command conditions 2 and 4.
The procedure byte ' 61xx ' notifies the TTL to issue the get response command to the IC card. The P3 of the GET Response command header is set to ≤ ' xx '.
Procedure byte ' 6Cxx ' notifies the TTL to immediately re-send the previous command, while the command header is p3= ' xx '.
Command conditions 2 and 4 in fault-tolerant processing, the use of procedure bytes is as follows. In the event of an error, the IC card echoes the error or warning status code instead of ' 61xx ' or ' 6Cxx '.
A. Order Situation 2
If the IC card receives a command condition 2 of the head and LE≠LICC, it should echo the process byte ' 6CLICC ' (or the status code indicates a warning or error condition, rather than SW1 sw2= ' 9000 ') to notify the TTL to immediately re-send the P3=LICC command header.
If the IC card receives the head of the command condition 2 and le= "LICC", the IC card is under Process byte control or loopback the requested data and related status code, or loopback process byte ' 61xx ' (or status code indicating a warning or error condition, rather than SW1 sw2= ' 9000 ') The notification TTL issues the Get response command by the maximum length of ' xx '.
B. Order situation 4
If the IC card receives a command Condition 4 command, after processing the data sent with C-APDU, should send back the process byte ' 61xx ', (or the status code indicating a warning or error condition, rather than SW1 sw2= ' 9000 ') notification TTL by maximum length ' xx ' issue get Response command.
(3) GET Response command
The TTL issue get response command is to obtain the data from the IC card corresponding to the C-APDU of the command conditions 2 and 4 of Le bytes. GET response only applies to t=0 protocol types.
The structure of the command message is as follows:
BYTE value
CLA ' 00 '
INS ' C0 '
P1 ' 00 '
P2 ' 00 '
Maximum length of Le expected data
After the normal processing is finished, the IC card loopback status Code SW1 sw2= ' 9000 ' and LICC bytes of data.
When an error condition occurs, the error status code (SW1 SW2) is encoded as follows:
SW1 SW2 meaning
' XX ' GET response failed
' 67 ' 00 ' Field length error
' 6A ' P1 p2≠ ' 00 '
3.2 t= "1 protocol" APDU transmission
The C-APDU is transferred from TAL to Ttl,ttl to map it to the INF domain of an I block of C-APDU, and then sends this I block to the IC card. IC Card in the INF domain of I block to the TTL loopback response data (if present) and the status code, the contents of the INF domain of block I are mapped to R-APDU, and then returned to Tal.
If necessary, the C-APDU and response data/status codes can be broken up into multiple chunks of the INF domain link.
4. Application Layer
The application protocol consists of an ordered set of data exchanges between Tal and TTL, and the subsequent sections of this section define the application protocol.
Each step of the application layer Exchange consists of a command response pair, in which Tal sends a command to the IC card via TTL, and the IC card processes the command and returns a response to TAL via TTL. Each specific command matches a specific response. A APDU is a command message or a response message. Both command messages and response messages can contain data, and the transport protocol manages the situation of four command cases with TTL, as follows:
data in APDU:
Situation command Data response data
1 NO No
2 None available
3 There is no
4 There are
It is important to note that there is a special message called Security message in the application layer, it adds the security data Mac on the basis of the original command, so there is always data to be sent to the IC card, so it is only applicable to order conditions 3 and 4. When a secure message is used, the command for condition 1 becomes case 3, and the command for Case 2 becomes case 4.
4.1 C-APDU
The C-APDU contains a mandatory four-byte command header, expressed in CLA, INS, P1, and P2, and includes a variable-length conditional body.
The command header is defined as follows:
* CLA: Instruction type, 1Byte, can be assigned any value except ' FF '.
* INS: Instruction type instruction code, 1Byte. INS works only if the low half byte is 0 and the high half is neither ' 6 ' nor ' 9 '.
* P1 P2: Parameter byte, respectively 1Byte, according to different ins to define.
The condition body is defined as follows:
* Lc accounts for one byte, which defines the number of bytes sent in the C-APDU. Lc values range from 1 to 255.
* Le takes one byte, indicating the maximum number of bytes expected to be returned in the R-APDU. Le has a value range from 0 to 255, and if le=0, the maximum length of bytes expected to return data is 256.
Four scenarios of possible C-APDU structures are as follows:
situation Structure
1 CLA INS P1 P2
2 CLA INS P1 P2 Le
3 CLA INS P1 P2 Lc Data
4 CLA INS P1 P2 Lc Data Le
4.2 R-APDU
R-APDU is a string of bytes that consists of a conditional body and a required two-byte status code SW1 SW2. The condition body is a string of data bytes whose maximum length is defined in Le in C-APDU. The required status code indicates the status of the IC card after the command has been processed.
SW1 SW2 Code follows the following rules:
* SW1 's high half byte is ' 6 ' or ' 9 ';
* The value of the forbidden SW1 is ' 60 ';
* When the value of SW1 is ' 61 ' or ' 6C ', it should be treated as an error;
* When the command ends normally, the value of SW1 SW2 is ' 9000 ';
* When the high half byte of SW1 is ' 9 ', and the low half byte is not ' 0 ', its meaning depends on the relevant application;
* When the high half byte of SW1 is ' 6 ' and the low half byte is not ' 0 ', the meaning of SW1 is irrelevant to the application.
Transfer Protocol for IC card (3) "Turn"