Abstract:
The present invention relates to the field of information security. Disclosed is an intelligent card secure communication method, capable of preventing the communication data from being monitored or intercepted. The method of the present invention provides a card with a definitely secure communication mechanism, initiates a session for the two communication parties to authenticate each other, and to check the identification legitimacy of the other party and the integrity of communication data, and provides a security constraint for subsequent communication, thus improving communication security.

Description:
FIELD 
       [0001]    The present invention relates to information security field, more particularly relates to a method for secure communication of a smart card. 
       BACKGROUND 
       [0002]    With rapid development of communication technology, information network is widely applied, and crimes of computer and network come up constantly. People protect the data of information network by using identity identification and encryption technology and smart card products are widely used as identify tools for user identity identification. Secure and effective using of smart card can implement encryption and decryption process in real time for data transferred in the network, so as to guarantee that the user can enjoy service provided by the information network with security, speediness and convenience. 
         [0003]    For the security related to the information security communication field, technology of the present is not perfect and cannot avoid problem of communication data being monitored or intercepted. 
       SUMMARY 
       [0004]    In order to solve problem in the conventional technology, the present invention provides a method for secure communication of smart card. 
         [0005]    The technical solution of the present invention is a method for secure communication of smart card, which comprises: 
         [0006]    S1 including powering up the smart card; 
         [0007]    S2 including setting a security identifier and a security variate; 
         [0008]    S3 including waiting for receiving a command and determining a type of a received command; 
         [0009]    if the received command is a first command, setting an authentication method identifier as an external authentication identifier or a mutual authentication identifier, setting an authentication identifier of a certificate, obtaining an RSA public key reference and an RSA private key reference, setting a security environment management identifier, going back to step S3; 
         [0010]    if the received command is a second command, determining whether both the security environment management identifier and the authentication identifier of the certificate are set, authenticating the certificate, setting the authentication identifier and going back to S3 if both the security environment management identifier and the authentication identifier of the certificate are set; reporting an error and going back to S2 if the security environment management identifier and the authentication identifier of the certificate are not both set; 
         [0011]    if the received command is a third command, determining whether the authentication identifier of the certificate is set, sending the certificate to an external device of the smart card and going back to S3 if the authentication identifier of the certificate is set; returning to S2 if the authentication identifier of the certificate is not set; 
         [0012]    if the received command is a fourth command, going to S4; if the received command is a fifth command, going to S5; if the received command is a sixth command, going to S6; if the received command is a seventh command, going to S7; 
         [0013]    if the command is one of other commands, determining whether the command meets a preset condition, authenticating the command according to a safe level and a session key if the command meets the preset condition; performing corresponding operation after a successful authentication, going back to S3; otherwise, reporting an error if the command does not meet the preset condition; 
         [0014]    S4 including determining whether a condition that the security environment management identifier is not set and the authentication identifier of the certificate is set is met; reporting an error and going back to S2 if the condition is met; decrypting the fourth command via a public key referenced by the RSA public key reference to obtain a decryption result; obtaining a TLV structure with a safe level from the decryption result, storing the safe level, establishing a session key, storing the session key, setting signature information and setting a session key establishing identifier, going back to S3; 
         [0015]    S5 including determining whether the session key establishment identifier is set, generating a first random number, storing the first random number and sending the first random number to the external device of the smart card and setting an obtaining random number identifier if the session key establishment identifier is set; going back to S3; reporting an error and going back to S2 if the session key establishment identifier is not set; 
         [0016]    S6 including determining whether the obtaining random number identifier is set, reporting an error and going back to step S2 if the obtaining random number identifier is not set; constructing a first data block containing the TLV structure with safe level, the session key, the first random number, performing an operation on the first data block with a first algorithm to obtain a first calculation result, constructing a second block data containing the first calculation result and the first algorithm, reading a signature result of the external device of the smart card in the sixth command, authenticating the signature result via a public key referenced by the RSA public key reference and the second data block; setting an external authentication identifier if the authentication is successful, determining whether an authentication method identifier is the external authentication identifier, setting the mutual authentication identifier if the authentication method identifier is the external authentication identifier, going back to S3; going back to S3 if the authentication method identifier is not the external authentication identifier; reporting error and going back to S2 if the authentication is not successful; 
         [0017]    S7 including obtaining a second random number from the seventh command, storing the second random number, determining whether a condition that the authentication method identifier is external authentication identifier and the external authentication identifier is set is met, constructing a third data block containing the session key and the second random number if the condition that the authentication method identifier is external authentication identifier and the external authentication identifier is set is met, performing an operation on the third data block via the first algorithm to obtain a second calculation result; constructing a fourth data block containing the second calculation result and the first algorithm; encrypting the fourth data block via a private public key referenced by the RSA private key reference to obtain an encryption result, wherein the obtained encryption result is a first signature result, sending the first signature result to the external device of the smart card, setting the mutual authentication identifier, clearing the first random number and the second random number, going back to S3; going back to S2 if the condition that the authentication method identifier is external authentication identifier and the external authentication identifier is set is not met. 
         [0018]    Setting the security identifier includes: resetting the security environment management identifier, setting the session security identifier being without a safe level, resetting the authentication identifier of the certificate, resetting the obtaining random number identifier, resetting the session key establishment identifier, resetting the external authentication identifier and resetting the mutual authentication identifier, setting the security variate includes: setting the RSA public key reference as a default RSA public key reference; setting the RSA private key reference as a default RSA private key reference; setting the first session key, the second session key, the third session key, the fourth session key and the fifth session key as a default value respectively. 
         [0019]    Setting an authentication method identifier as an external authentication identifier or a mutual authentication identifier includes: setting the authentication method identifier as external authentication identifier if a value of a first preset byte of the first command is a first preset value; setting the authentication method identifier as mutual authentication identifier if the value of the first preset byte of the first command is a second preset value; 
         [0020]    setting an authentication identifier of a certificate includes: resetting the authentication identifier of the certificate if a value of a third preset byte of the first command is a third preset value; setting authentication identifier of the certificate if the value of the third preset byte of the first command is a fourth preset value; 
         [0021]    obtaining an RSA public key reference and an RSA private key reference includes:
       step a1 including determining whether a first TLV structure in a data field of the first command is a first preset structure;   going to step a2 if the first TLV structure in the data field of the first command is the first preset structure; otherwise, reporting an error and going back to S2;   step a2 including determining whether there is a TLV structure being a second preset structure in the data field;   obtaining and storing the RSA public key reference if there is a TLV structure being a second preset structure in the data field; reporting an error and going back S2 if there is not a TLV structure being a second preset structure in the data field;   step a3 including determining whether there is a TLV structure being a third preset structure in the data field;   obtaining and storing RSA private key reference if there is a TLV structure being a third preset structure in the data field; reporting an error and going back to S2 if there is not a TLV structure being a third preset structure in the data field.       
 
         [0028]    Obtaining and storing the RSA public key reference includes: obtaining the last two bytes of a first structure value and storing the last two bytes as the RSA public key reference if a value of the second preset structure is the first structure value; using the default RSA public key by the card if the value of the second preset structure is a second structure value; 
         [0029]    obtaining and storing the RSA private key reference includes: obtaining the last two bytes of a third structure value and storing the last two bytes as the RSA private key reference if a value of a third preset structure is the third structure; using the default RSA private key reference by the card if the value of the third preset structure is a fourth structure. 
         [0030]    After determining whether both the security environment management identifier and the authentication identifier of the certificate are set, the method further includes: determining whether the second command meets a first condition, using a public key authentication certificate referenced by the RSA public key to determine whether there is a certificate required to be authenticated if the second command meets the first condition, going back to S2 if there is a certificate required to be authenticated or setting the authentication identifier of the certificate if there is not a certificate required to be authenticated and going back to S3; going back to S2 if the second command does not meet the first condition. 
         [0031]    That the second command meets the first condition includes: a value of a specified part of the second command equals to a fifth preset value; 
         [0032]    determining whether there is a certificate required to be authenticated includes: there is not a certification required to be authenticated if a value of a fifth bit of a first byte of the second command is a ninth preset value; there is a certificate required to be authenticated if the value of the fifth bit of the first byte of the second command is not the ninth preset value. 
         [0033]    Before decrypting the fourth command via the public key referenced by the RSA public key reference, the method further includes: determining whether a length of a data field of the fourth command equals to a modular length of the public key referenced by the RSA public key reference, decrypting the fourth command with the public key referenced by the RSA public key reference if the length of the data field of the fourth command equals to the modular length of the public key referenced by the RSA public key reference; otherwise, reporting an error and going back to S3; 
         [0034]    after storing the safe level and before establishing the session key, the method further includes: determining whether the session key can be established, establish session key if the session key can be established; otherwise, reporting an error and going back to S3. 
         [0035]    Determining whether the session key can be established includes: 
         [0036]    determining whether a condition that a value of a general tag of a control reference template equals to a seventh preset value, and there is a first TLV structure and a second TLV structure the length of which equals to a first preset length in the control reference template is met, the session key can be established if the condition is met; the session key cannot be established if the condition is not met; wherein the seventh preset value is a first tag or a second tag; 
         [0037]    establishing the session key includes: in the case that the value of the general tag of the control reference template is a first tag, 
         [0038]    if a Value in the TLV structure with a TAG being 95 is a first value, establishing a first session key; 
         [0039]    if the Value in the TLV structure with the TAG being 95 is a second value, establishing a second session key; 
         [0040]    if the Value in the TLV structure with the TAG being 95 is a third value, establishing the first session key and the second session key; 
         [0041]    in the case that the value of the general tag of the control reference template is a second tag, 
         [0042]    if the Value in the TLV structure with the TAG being 95 is a first value, establishing a third session key; 
         [0043]    if the Value in the TLV structure with the TAG being 95 is a second value, establishing a fourth session key; 
         [0044]    if the Value in the TLV structure with the TAG being 95 is a third value, establishing the third session key and the fourth session key; 
         [0045]    if the Value in the TLV structure with the TAG being 95 is a fourth value, establishing a fifth session key; 
         [0046]    after storing the session key and before setting signature information, the method further includes: 
         [0047]    determining whether the signature information is required to be set, setting signature if the signature information is required to be set; reporting an error and going back to S2 if the signature information is not required to be set. 
         [0048]    Determining whether the session key can be established includes: 
         [0049]    determining whether the condition that the value of the general tag of the control reference template equals to the seventh preset value and there is the first TLV structure and the second TLV structure the length of which equals to the first preset length in the control reference template is met, the session key can be established if the condition is met; otherwise, the session key can not be established; 
         [0050]    determining whether the signature information is required to be set includes: 
         [0051]    determining whether there is a third TLV structure in the that control reference template, wherein a value of TAG of third TLV structure equals to an eighth preset value and a length of TAG of third TLV structure equals to an second preset length; the signature information being required to be set if there is a third TLV structure in the that control reference template; otherwise, the signature information not being required to be set. 
         [0052]    S4-S7 are replaced by S4′-S7′, 
         [0053]    S4′, reporting an error and going back to S2; 
         [0054]    S5′, determining whether a condition that the security environment management identifier is not set and the authentication identifier of the certificate is set is met, reporting an error if the condition is met, going back to S2; otherwise, generating a first random number, storing the first random number and sending the first random number to an external device of the smart card and setting an obtaining random number identifier, going back to S3; 
         [0055]    S6′, determining whether a condition that the obtaining random number identifier is set, and a decryption result obtained by decrypting the data field of the sixth command with the public key referenced by the RSA public key reference is successfully authenticated is met, reporting an error and going back to S2 if the condition is not met; if the condition is met, obtaining a safe level from the decryption result and generating card confidential data, determining whether a control reference template of the decryption result meets a preset requirement, obtaining and storing confidential data of the external device of the smart card if the control reference template meets the preset requirement, generating a session key according to the card confidential data and the confidential data of the external device, generating signature information according to a specified byte of the confidential data of the external device and a specified byte of the card confidential data, setting a session key establishment identifier and setting an obtaining random number identifier; reporting an error and going back to S2 if the control reference template does not meet the preset requirement. 
         [0056]    S7′, obtaining the second random number from the seventh command, storing the second random number and determining whether a condition that the session key establishment identifier is set and the obtaining random number identifier is set is met, going back to S2 if the condition is not met; if the condition is met, obtaining a data field of the seventh command from the seventh command, constructing a third data block containing the card confidential data and the data field of the seventh command, performing operation on the third data block via the first algorithm to obtain an operation result, wherein the operation result is a second calculation result; constructing a fourth data block containing the card confidential data and the second calculation result; encrypting the fourth block via the private key referenced by the RSA private key reference to obtain an encryption result, wherein the encryption result is a first signature result; sending the first signature result to the external device of the smart card, setting the mutual authentication identifier, clearing the first random number and the second random number, going back S3. 
         [0057]    Generating a session key according to the card confidential data and the confidential data of the external device includes 
         [0058]    performing exclusive OR operation on the confidential data of the external device and the card confidential data to obtain an exclusive OR operation result; setting a corresponding counting number according to a type of the session key required to be generated, splicing the exclusive operation result and the counting number orderly, performing an operation on an splicing result via a first algorithm to obtain a third calculation result, obtaining a preset byte data from the third calculation result and storing the preset byte data as the session key. 
         [0059]    The advantages of the technical solution provided by the embodiments of the present invention are: by the method of the present invention, a security communication mechanism can be provided safely to a card; By initiating a session, the two parties of communication can authenticate with each other and one party can determine legitimate identity of the other party and the integrity of the other party, which provides safe restraint for subsequent communication so as to improve safety of communication. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0060]      FIGS. 1-3  are flowcharts for secure communication of smart card provided by an embodiment of the present invention. 
       
    
    
     DETAILED DESCRIPTION 
       [0061]    The embodiment of the present invention provides a method for secure communication of smart card based on management of secure channels in a smart card. Details of the embodiment will be illustrated by combining drawings of the present invention. 
         [0062]    Referring to  FIGS. 1-3 , an embodiment of a method for secure communication of smart card is provided. The method includes step  101  to step  112 . 
         [0063]    In step  101 , a smart card is powered up. 
         [0064]    In step  102 , a security identifier and a security variate are set. 
         [0065]    In the embodiment, the secure identifier may be set by: setting a security environment management identifier to an uncompleted state, setting an initial state of a session security identifier as an identifier without a safe level, setting a completing certification authentication identifier to an uncompleted state, setting an obtaining random number identifier to an uncompleted state, setting a session key identifier to an un-established state, setting an external authentication identifier to an un-completed state and setting a mutual authentication identifier to an un-completed state. 
         [0066]    In the embodiment, the security variate may be set by: setting a RSA public key reference as PKV1 (Public Key Version 1), PKI1 (Public Key Index 1); setting a RSA private key reference as SKV1 (Secret Key Version 1), SKI1 (Secret Key Index); setting a first session key, a second session key, a third session key, a fourth session key and a fifth session key as an initial value which can be ‘00’ of 16 bytes; setting signature information as an initial value which can be ‘00’ of 8 bytes. Preferably, the first session key is CMAC (Command Message Authentication Code) session key, the second session key is RMAC (Response Message Authentication Code) session key, the third session key is CENC(Command Encryption) session key, the fourth session key is RENC(Response Encryption) session key and the fifth session key is DEK (Data Encryption Key) session key. 
         [0067]    In step  103 , a command is waited to be received. 
         [0068]    In the embodiment, the command includes a first command, a second command, a third command, a fourth command, a fifth command, a sixth command, a seventh command and other commands. 
         [0069]    Preferably, the first command is a command of security environment management, the second command is a command of executing a security operating certificate authentication, the third command is a command of obtaining a certificate, the fourth command is a command of executing a security operating decryption, the fifth command is a command of obtaining a random number, the sixth command is a command of an external authentication, the seventh command is a command of an internal authentication. 
         [0070]    In step  104 , a type of the received command is determined. 
         [0071]    In the embodiment, the type of the received command is determined according to a value from a second byte to a fourth byte of the command, which includes: 
         [0072]    if the value from the second byte to the fourth byte of the command is 2281A4 or 22C1A4 or 2281B6 or 22C1B6, the command is the first command; going to step  105 ; 
         [0073]    if the value from the second byte to the fourth byte in the command is 2A00AE or 2A00BE, the command is the second command; step  113  is executed; 
         [0074]    if the value from the second byte to the fourth byte in the command is CA7F21 or CB7F21, the command is the third command, step  116  is executed; 
         [0075]    if the value from the second byte to the fourth byte in the command is 2A8084, the command is the fourth command, step  118  is executed; 
         [0076]    if the value from the second byte to the fourth byte in the command is 840000, the command is the fifth command, step  128  is executed; 
         [0077]    if the value from the second byte to the fourth byte in the command is 820000, the command is the sixth command, step  131  is executed; 
         [0078]    if the value from the second byte to the fourth byte is 880000, the command is the seventh command, step  140  is executed; 
         [0079]    if the command is one of other commands, step  146  is executed. 
         [0080]    In step  105 , a value of a third byte of the first command is determined; if the value is equal to a first preset value, step  106  is executed; if the value is equal to a second preset value, step  107  is executed. 
         [0081]    In the embodiment, preferably, the first preset value is 81, the second preset value is C1. 
         [0082]    In step  106 , an authentication method identifier is set as an external authentication identifier, step  108  is executed. 
         [0083]    In step  107 , the authentication method identifier is set as a mutual authentication identifier. 
         [0084]    In step  108 , a value of the fourth byte in the first command is determined, if the value is equal to a third preset value, step  109  is executed; if the value is equal to the fourth preset value, step  110  is executed. 
         [0085]    In the embodiment, preferably, the third preset value is A4; the fourth preset value is B6. 
         [0086]    In step  109 , an authentication identifier of a certificate is set to a non-required authentication state, step  111  is executed; 
         [0087]    In step  110 , the authentication identifier of a certificate is set to a required authentication state. 
         [0088]    In step  111 , a RSA public key reference and a RSA private key reference are obtained and determine whether the RSA public key reference and the RSA private key reference are successfully obtained; step  112  is executed if the RSA public key reference and the RSA private key reference are successfully obtained; error information is output, and step  102  is executed. 
         [0089]    In the embodiment, the RSA public key reference and the RSA private key reference are obtained via step a1 to step a3. 
         [0090]    In step a1, whether a first TLV structure in a data field meets a first preset structure is determined; step a2 is executed if the first TLV structure in the data field meets the first preset structure; error information is output and step  102  is executed if the first TLV structure in the data field does not meet the first preset structure. 
         [0091]    The first preset structure may be 80021002. 
         [0092]    In step a2, whether a TLV structure in the data field meets a second preset structure is determined; the RSA public key reference is obtained if the TLV structure in the data field meets the second preset structure; error information is output and step  102  is executed if the TLV structure in the data field does not meet the second preset structure. 
         [0093]    The second preset structure may be 8302N1N2 or 8300. 
         [0094]    In the case that the second preset structure is 8302N1N2, N1 and N2 are obtained and stored as the RSA public key reference, where N1 indicates an RSA public key version number and N2 indicates an RSA public key index number. 
         [0095]    In the case that the second preset structure is 8300, the default RSA public key reference of PKV1 and PKI1 are used by the smart card, where PKV1 indicates an RSA public key version number and PKI1 indicates an RSA public key index number. 
         [0096]    In step a3, whether a TLV structure in the data field meets a third preset structure is determined; the RSA private key reference is obtained if the TLV structure in the data field meets the third preset structure; error information is output and step  102  is executed if the TLV structure in the data field does not meet the third preset structure. 
         [0097]    The third preset structure may be 8402N3N4 or 8400. 
         [0098]    In the case that the third preset structure is 8402N3N4, N3 and N4 are obtained and stored as the RSA private key reference, where N3 indicates a private key version number, N4 indicates a private key index number. 
         [0099]    In the case that the third preset structure is 8400, the default RSA private key reference of SKV1 and SKI1 are used by the smart card, where SKV1 indicates a private key version number, SKI1 indicates a default private key index number. 
         [0100]    In step  112 , the security environment identifier is set to a completed state, step  103  is executed. 
         [0101]    Step  113  to step  115  are executed if the second command is received. 
         [0102]    In step  113 , whether the second command meets a first preset condition is determined, step  114  is executed if the second command meets the first preset condition; error information is output and step  102  is executed if the second command does not meet the first preset condition. 
         [0103]    In the embodiment, specifically, determine whether the second command meets the following conditions: 
         [0104]    1) security environment management identifier is in the completed state; 
         [0105]    2) authentication identifier of the certificate is in the required authentication state; 
         [0106]    3) the value of a specified part of the second command is equal to a fifth preset value, where the specified part in the second command is TAG (a certificate tag) part of the TLV structure, the fifth preset value may be 7F21; 
         [0107]    4) the certificate is successfully authenticated by using the public key referenced by the RSA public key reference. 
         [0108]    If all of the conditions above are met, it&#39;s determined that the second command meets the first preset condition; otherwise, it&#39;s determined that the second command does not meet the first preset condition. 
         [0109]    In step  114 , whether there is a certificate required to be authenticated is determined; step  103  is executed if there is a certificate required to be authenticated; otherwise, step  115  is executed. 
         [0110]    In the embodiment, whether there is a certificate required to be authenticated is determined according to a value of a fifth bit of a first byte in the second command. If the value of the fifth bit of the first byte in the second command is not equal to 0, it&#39;s indicated that there is a certificate required to be authenticated; if the value of the fifth bit of the first byte in the second command is equal to 0, it&#39;s indicated that there is not a certificate required to be authenticated. 
         [0111]    In step  115 , the authentication identifier of the certificate is set to a completed state and step  103  is executed. 
         [0112]    Step  116  to step  117  are executed if the third command is received. 
         [0113]    In step  116 , whether the authentication identifier of the certificate is in a completed state is determined; if the authentication identifier of the certificate is in a completed state, step  117  is executed; otherwise, error information is output and step  102  is executed. 
         [0114]    In step  117 , the certificate is sent to the external device of the smart card and step  103  is executed. 
         [0115]    In the embodiment, the certificate stored in the security field of the smart card is sent to the external device of the smart card. 
         [0116]    As shown in  FIGS. 1-2 , step  118  to step  127  are executed if the fourth command is received. 
         [0117]    In step  118 , whether the security environment management identifier is in the completed state is determined; if the security environment management identifier is in the completed state, step  120  is executed; otherwise, step  119  is executed. 
         [0118]    In step  119 , whether the authentication identifier of the certificate is in the required authentication state is determined; if the authentication identifier of the certificate is in the required authentication state, error information is output and step  102  is executed; if the authentication identifier of the certificate is not in the required authentication state, step  120  is executed. 
         [0119]    In step  120 , whether the fourth command meets a third preset condition is determined; if the fourth command meets the third preset condition, step  121  is executed; if the fourth command does not meet the third preset condition, error information is output and step  103  is executed. 
         [0120]    In the embodiment, the fourth command meets the third preset condition if a length of the data field of the fourth command equals to a modular length of the public key referenced by the RSA public key reference; otherwise, the fourth command does not meet the third preset condition. 
         [0121]    In step  121 , the fourth command is decrypted by using the public key referenced by the RSA public key reference. 
         [0122]    In the embodiment, specifically, the public key referenced by the RSA public key is used to decrypt the data field of the fourth command via a preset way. 
         [0123]    In step  122 , whether a decryption result is obtained successfully is determined; if yes, step  123  is executed; if no, error information is output and step  103  is executed. 
         [0124]    In the embodiment, if a value from a first byte to a second byte of the decryption result equals to a sixth preset value, correct decryption result is obtained. Preferably, the sixth preset value is 0002. 
         [0125]    In step  123 , whether a TLV structure with a safe level is obtained successfully is determined; if yes, step  125  is executed; otherwise error information is output and step  103  is executed. 
         [0126]    In the embodiment, the TLV structure with the safe level is obtained from the decryption result. Whether the TLV structure with the safe level is obtained successfully is determined according to the value of the first two bytes of the TLV structure with the safe level. If the value of the first two bytes of TLV structure with the safe level is D301, it&#39;s indicated that the TLV structure with the safe level is successfully obtained; otherwise, it&#39;s indicated that the TLV structure with the safe level is unsuccessfully obtained. 
         [0127]    In step  124 , the safe level is stored and whether a session key can be established is determined; if the session key can be established is determined, step  125  is executed; otherwise, error information is output, and step  103  is executed. 
         [0128]    In the embodiment, a value of a third byte of the TLV structure with safe level is stored as the safe level. 
         [0129]    The session key can be established if a value of a general tag of a CRT control reference template equals to a seventh preset value, there is a first TLV structure in the control reference template and there is a second TLV structure, the length of which is equal to a first preset length, in the control reference template; otherwise, the session key can not be established. 
         [0130]    Preferably, the seventh preset value is B4 or B8; the first TLV structure is a TLV structure, a value of the TAG of which equals to 95; the second TLV structure is a TLV structure, a value of the TAG of which equals to D1; the first preset length is 16. 
         [0131]    In step  125 , the session key is established and stored. 
         [0132]    In the embodiment, a corresponding session key is established according to a value of the general tag of the CRT control reference template; in the case that the value of the general tag of CRT control reference template is B4, 
         [0133]    if a Value in TLV structure with a TAG being 95 is 10, D1 is established as a first session key of TLV structure data of TAG; 
         [0134]    if the Value in TLV structure with a TAG being 95 is 20, D1 is established as a second session key of TLV structure data of TAG; 
         [0135]    if the Value in TLV structure with a TAG being 95 is 30, D1 is established as the first session key and the second session key of TLV structure data of TAG 
         [0136]    if the Value in TLV structure with a TAG being 95 meets any one of the conditions above, it&#39;s indicated that the session key is established and stored successfully; otherwise, it&#39;s indicated that the session key is not established and stored successfully. 
         [0137]    In the case that the value of the general tag of CRT control reference template is B8, if the Value in TLV structure with a TAG being 95 is 10, D1 is established as a third session key of TLV structure data of TAG; 
         [0138]    if the Value in TLV structure with a TAG being 95 is 20, D1 is established as a fourth session key of TLV structure data of TAG; 
         [0139]    if the Value in TLV structure with a TAG being 95 is 30, D1 is established as the third session key and the fourth session key of TLV structure data of TAG; 
         [0140]    if the Value in TLV structure with a TAG being 95 is C0, D1 is established as the fifth session key of TLV structure data of TAG 
         [0141]    In step  126 , whether the signature information is required to be set is determined; step  127  is executed if the signature information is required to be set; error information is output and step  102  is executed if the signature information is not required to be set. 
         [0142]    In the embodiment, if there is a third TLV structure, of which the value of TAG equals to 91 and the length equals to a second preset length, in CRT control reference template, the signature information is required to be set. Preferably, the second preset length is 8. 
         [0143]    In step  127 , the signature information is set and the session key establishing identifier is set to a completed state, step  103  is executed. 
         [0144]    In the embodiment, the signature information is set as data of 8 bytes, preferably, the data of 8 bytes may be ‘0000000000000000’. 
         [0145]    Step  128  to step  130  are executed if the fifth command is received. 
         [0146]    In step  128 , whether the fifth command meets a fourth preset condition is determined; step  129  is executed if the fifth command meets a fourth preset condition; otherwise, error information is output, and step  102  is executed. 
         [0147]    In the embodiment, if the session key establishment identifier is in a completed state, it&#39;s determined that the fifth command meets the fourth preset condition; otherwise, it&#39;s determined that the fifth command does not meet the fourth preset condition. 
         [0148]    In step  129 , a first random number is generated and stored, and the first random number is sent to an external device of the smart card. 
         [0149]    In the embodiment, preferably, the first random number may be a random number with 16 bytes. 
         [0150]    In step  130 , the obtaining random number identifier is set to a completed state, and step  103  is executed. 
         [0151]    Step  131  to step  139  are executed if the sixth command is received. 
         [0152]    In step  131 , whether the obtaining random number identifier is in a completed state is determined; step  132  is executed if the obtaining random number identifier is in the completed state; otherwise, error information is output, step  102  is executed. 
         [0153]    In step  132 , a first data block containing the TLV structure with safe level, the session key and the first random number is constructed. 
         [0154]    In step  133 , the first data block is operated via a first algorithm and a first calculating result is obtained. 
         [0155]    In the present embodiment, preferably, the first algorithm may be an algorithm of SHA1. 
         [0156]    In step  134 , a second data block containing the first calculating result and the first algorithm is constructed. 
         [0157]    In step  135 , a signature result of the external device of the smart card is read from the sixth command and the signature result is verified via the public key referenced by the RSA public key reference and the second data block. 
         [0158]    In step  136 , whether the signature result is successfully verified is determined; step  137  is executed if the signature result is successfully verified; otherwise, error information is output and step  102  is executed. 
         [0159]    In step  137 , the external authentication identifier is set to a completed state. 
         [0160]    In step  138 , whether the authentication method identifier is the external authentication identifier; step  139  is executed if the authentication method identifier is the external authentication identifier; otherwise, step  103  is executed. 
         [0161]    In step  139 , the mutual authentication identifier is set to a completed state, and step  103  is executed. 
         [0162]    As shown in  FIG. 3 , step  140  to step  145  are executed if the seventh command is received. 
         [0163]    In step  140 , a second random number is read from the seventh command, the second random number is stored and whether the seventh command meets a fifth preset condition is determined; step  141  is executed if the seventh command meets the fifth preset condition; otherwise, error information is output and step  102  is executed. 
         [0164]    In the embodiment, the first 16 bytes in the seventh command are read as the second random number. 
         [0165]    It&#39;s determined that whether the seventh command meets both the following conditions: 
         [0166]    1) the authentication method identifier is the external authentication identifier; 
         [0167]    2) the external authentication identifier is in a completed state. 
         [0168]    If both of above conditions are met, it&#39;s determined that the seventh command meets the fifth preset condition; otherwise, it&#39;s determined that the seventh command does not meet the fifth preset condition. 
         [0169]    In step  141 , a third data block containing session key and the second random number is constructed. 
         [0170]    In step  142 , the third data block is operated via the first algorithm and a second calculation result is obtained. 
         [0171]    In step  143 , a fourth data block containing the second calculation result and the first algorithm is constructed. 
         [0172]    In step  144 , the fourth data block is encrypted with the private key referenced by the RSA public key reference and a first signature result is obtained and the first signature result is sent to the external device of the smart card. 
         [0173]    In step  145 , the mutual authentication identifier is set to a completed state, the first random number and the second random number are cleared; and step  103  is executed. 
         [0174]    If the command is one of other commands, the following operations are executed. 
         [0175]    In step  146 , whether the command meets a sixth preset condition is determined; step  147  is executed if the command meets the sixth preset condition; a corresponding operation is operated if the command does not meet the sixth preset condition and step  103  is executed. 
         [0176]    In the embodiment, if a value of a third bit in a first byte of the command equals to a preset value, it&#39;s determined that the command meets the sixth preset condition; otherwise, the command does not meet the sixth preset condition. Preferably, the preset value is 0. 
         [0177]    In step  147 , whether the mutual authentication identifier is in a completed state; step  148  is executed if the mutual authentication identifier is in the completed state; otherwise, error information is output. 
         [0178]    In step  148 , the safe level is obtained, step  149  is executed if the safe level indicates that there is a command message authentication code and the data field is required to be encrypted; step  153  is executed if the safe level only indicates that there is the command message authentication code; a corresponding operation is performed and step  103  is executed if the safe level indicates that there is not a command message authentication code and the data field is not required to be encrypted. 
         [0179]    In step  149 , command message authentication code is obtained from the command, a first session key and a third session key is obtained according to the safe level, signature information is encrypted via a specified part of the first session key and first encryption information is obtained. 
         [0180]    In the embodiment, the command message authentication code is obtained from the specified TLV structure of the command. The specified part of the first session key is the first 8 bytes of the first session key. 
         [0181]    In step  150 , a first data containing the first session key, the first encryption information and the first specified part of the command is constructed, the first data is taken as an input of a first specified algorithm so as to complete a first data signature to obtain a first data signature result. 
         [0182]    In the embodiment, the first specified part of the command is the data before the specified TLV structure and the data in the specified TLV structure. Preferably, the first specified algorithm is IS09797M2_ALG3. 
         [0183]    In step  151 , whether the first data signature result equals to a command message authentication code is determined; step  152  is executed if the first data signature result equals to the command message authentication code; error information is output if the first data signature result does not equal to the command message authentication code. 
         [0184]    In step  152 , the signature information as an integer is incremented by 1, the second specified part of the command is modified, decrypt the third specified part of the command via a second specified algorithm applied by the third session key and a corresponding operation is performed; step  103  is executed. 
         [0185]    In the embodiment, the second specified part is the length of the data field of the command. Preferably, the second specified algorithm is IS09797M2. For example, the data in 87L01XX in the data field of the command is decrypted. 
         [0186]    In step  153 , a command message authentication code is obtained from the command, the first session key is obtained according to the safe level, the signature information encrypt via the specified part of the first session key to obtain second encryption information. 
         [0187]    In step  154 , a second data containing the first session key, the second encryption information and the first specified part of the command is constructed, the second data is taken as the input of the first specified algorithm, signature of the second data is completed to obtain a signature result of the second data. 
         [0188]    In step  155 , whether the signature result of the second data equals to the command message authentication code is determined; step  156  is executed if the signature result of the second data equals to the command message authentication code; error information is output if the signature result of the second data does not equal to the command message authentication code. 
         [0189]    In step  156 , signature information as an integer is incremented by 1; the second specified part of the command is modified; and a corresponding operation is performed. 
         [0190]    It should be noted that, in the embodiment of the present invention, steps  118  to  127  are replaced with step of outputting error information and then going back to step  102 ; step  128  is replaced with step  128 ′, steps  131  to  134  are replaced with steps  131 ′ to  134 ′; steps  135  to  139  are replaced by setting the session key establishing identifier and obtaining random number identifier to completed states, going back to step  103 ; steps  140  to  141  are replaced with steps  140 ′ to  141 ′; step  143  is replaced with step  143 ′. 
         [0191]    In step  128 ′, safe environment managing identifier and authentication identifier of the certificate are determined; an error is reported if the safe environment managing identifier is in an uncompleted state and the authentication identifier of the certificate is in required authentication state; otherwise, steps  129  to  130  are executed. 
         [0192]    In step  131 ′, whether the sixth command meets a preset condition is determined; specifically, whether following conditions are met are determined: obtaining random number identifier being in a completed state; the encryption result obtained by decrypting the data field of the sixth command by the public key referenced by RSA public key reference being successfully authenticated; step  132 ′ is executed if the conditions are met; otherwise, an error is reported and step  102  is executed. 
         [0193]    In step  132 ′, the safe level is obtained from the encryption result and card confidential data is generated; where the card confidential data may be a random number with 32 bytes. 
         [0194]    In step  133 ′, whether the CRT control reference template of the decryption result meets a preset requirement is determined; step  134 ′ is executed if the CRT control reference template of the decryption result meets the preset requirement; otherwise, error information is output, step  102  is executed. The preset requirement is that the general TAG is 0xB4 or 0xB8; the next TLV is 9501XX, existed TLV is 80LXX and the value of L is 0 OR 1; the existed TLV is D1LXX and the value of L is 0; the existed TLV is 91LXX and the value of L is 0. 
         [0195]    In step  134 ′, confidential data of the external device of the smart card is obtained and stored, a session key is generated according to the card confidential data and the confidential data of the external device of the smart card, the confidential data of the external device of the smart card and the first 4 bytes of the card confidential data are spliced, the spliced result is stored as signature information. 
         [0196]    In the embodiment, that the session key is generated according to the card confidential data and the confidential data of the external device of the smart card specifically includes: an exclusive OR operation is carried on the confidential data of the external device of the smart card and the card confidential data to obtain an exclusive OR operation result, accounting number with 32 bytes is set according to a type of the session key that needs to be generated, the exclusive OR operation result and the counting number is spliced orderly and a SHA1 operation is made on the spliced result to obtain a third calculation result, the first 16 bytes of the third calculation result is obtained and the first 16 bytes is stored as the session key. 
         [0197]    In the case that the counting number is set as 1, the session key generated is a first session key; in the case that the counting number is set as 2, the session key generated is a second session key; in the case that the counting number is set as 3, the session key generated is a third session key; in the case that the counting number is set as 4, the session key generated is a fourth session key; in the case that the counting number is set as 5, the session key generated is a fifth session key. 
         [0198]    In step  140 ′, whether the condition that the session key establish identifier is in a completed state and the obtaining random number identifier is in a completed state is met is determined, step  141 ′ is executed if the condition is met; an error is reported and step  102  is executed if the condition is not met. 
         [0199]    In step  141 ′, a data field of the seventh command is obtained from the seventh command, a third data block containing card confidential data and the data field of the seventh command is constructed; then step  142  is executed. 
         [0200]    In step  143 ′, a fourth data block containing card confidential data and the second calculation result is constructed; steps  144  to  145  are executed. 
         [0201]    The embodiment above can also implement the purpose of the present invention. 
         [0202]    The described embodiments are only preferred embodiments of the invention and the embodiments are not intended to limit the invention. Any alteration or change easily obtained by those skilled in the art based on the invention should fall in the scope of protection of the invention. Therefore, the invention intends to protect what is claimed in claims.