Robert Relyea | 111a38b | 2010-11-28 16:36:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Implement the 7816 portion of the card spec |
| 3 | * |
| 4 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 5 | * See the COPYING.LIB file in the top-level directory. |
| 6 | */ |
| 7 | #ifndef CARD_7816T_H |
| 8 | #define CARD_7816T_H 1 |
| 9 | |
| 10 | typedef unsigned short vcard_7816_status_t; |
| 11 | |
| 12 | struct VCardResponseStruct { |
| 13 | unsigned char *b_data; |
| 14 | vcard_7816_status_t b_status; |
| 15 | unsigned char b_sw1; |
| 16 | unsigned char b_sw2; |
| 17 | int b_len; |
| 18 | int b_total_len; |
| 19 | enum VCardResponseBufferType { |
| 20 | VCARD_MALLOC, |
| 21 | VCARD_MALLOC_DATA, |
| 22 | VCARD_MALLOC_STRUCT, |
| 23 | VCARD_STATIC |
| 24 | } b_type; |
| 25 | }; |
| 26 | |
| 27 | #define VCARD_RESPONSE_NEW_STATIC_STATUS(stat) \ |
| 28 | static const VCardResponse VCardResponse##stat = \ |
| 29 | {(unsigned char *)&VCardResponse##stat.b_sw1, (stat), ((stat) >> 8), \ |
| 30 | ((stat) & 0xff), 0, 2, VCARD_STATIC}; |
| 31 | |
| 32 | #define VCARD_RESPONSE_NEW_STATIC_STATUS_BYTES(sw1, sw2) \ |
| 33 | static const VCardResponse VCARDResponse##sw1 = \ |
| 34 | {(unsigned char *)&VCardResponse##name.b_sw1, ((sw1) << 8 | (sw2)), \ |
| 35 | (sw1), (sw2), 0, 2, VCARD_STATIC}; |
| 36 | |
| 37 | /* cast away the const, callers need may need to 'free' the |
| 38 | * result, and const implies that they don't */ |
| 39 | #define VCARD_RESPONSE_GET_STATIC(name) \ |
| 40 | ((VCardResponse *)(&VCardResponse##name)) |
| 41 | |
| 42 | typedef enum { |
| 43 | VCARD_7816_ISO, |
| 44 | VCARD_7816_RFU, |
| 45 | VCARD_7816_PTS, |
| 46 | VCARD_7816_PROPIETARY |
| 47 | } VCardAPDUType; |
| 48 | |
| 49 | |
| 50 | /* |
| 51 | * 7816 header. All APDU's have this header. |
| 52 | * They must be laid out in this order. |
| 53 | */ |
| 54 | struct VCardAPDUHeader { |
| 55 | unsigned char ah_cla; |
| 56 | unsigned char ah_ins; |
| 57 | unsigned char ah_p1; |
| 58 | unsigned char ah_p2; |
| 59 | unsigned char ah_Le; |
| 60 | unsigned char ah_body[1]; /* indefinate length */ |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * 7816 APDU structure. The raw bytes are stored in the union and can be |
| 65 | * accessed directly through u.data (which is aliased as a_data). |
| 66 | * |
| 67 | * Names of the fields match the 7816 documentation. |
| 68 | */ |
| 69 | struct VCardAPDUStruct { |
| 70 | int a_len; /* length of the whole buffer, including header */ |
| 71 | int a_Lc; /* 7816 Lc (parameter length) value */ |
| 72 | int a_Le; /* 7816 Le (expected result length) value */ |
| 73 | unsigned char *a_body; /* pointer to the parameter */ |
| 74 | int a_channel; /* decoded channel */ |
| 75 | int a_secure_messaging; /* decoded secure messaging type */ |
| 76 | int a_type; /* decoded type from cla (top nibble of class) */ |
| 77 | VCardAPDUType a_gen_type; /* generic type (7816, PROPRIETARY, RFU, etc) */ |
| 78 | union { |
| 79 | struct VCardAPDUHeader *header; |
| 80 | unsigned char *data; |
| 81 | } u; |
| 82 | /* give the subfields a unified look */ |
| 83 | #define a_header u.header |
| 84 | #define a_data u.data |
| 85 | #define a_cla a_header->ah_cla /* class */ |
| 86 | #define a_ins a_header->ah_ins /* instruction */ |
| 87 | #define a_p1 a_header->ah_p1 /* parameter 1 */ |
| 88 | #define a_p2 a_header->ah_p2 /* parameter 2 */ |
| 89 | }; |
| 90 | |
| 91 | /* 7816 status codes */ |
| 92 | #define VCARD7816_STATUS_SUCCESS 0x9000 |
| 93 | #define VCARD7816_STATUS_WARNING 0x6200 |
| 94 | #define VCARD7816_STATUS_WARNING_RET_CORUPT 0x6281 |
| 95 | #define VCARD7816_STATUS_WARNING_BUF_END_BEFORE_LE 0x6282 |
| 96 | #define VCARD7816_STATUS_WARNING_INVALID_FILE_SELECTED 0x6283 |
| 97 | #define VCARD7816_STATUS_WARNING_FCI_FORMAT_INVALID 0x6284 |
| 98 | #define VCARD7816_STATUS_WARNING_CHANGE 0x6300 |
| 99 | #define VCARD7816_STATUS_WARNING_FILE_FILLED 0x6381 |
| 100 | #define VCARD7816_STATUS_EXC_ERROR 0x6400 |
| 101 | #define VCARD7816_STATUS_EXC_ERROR_CHANGE 0x6500 |
| 102 | #define VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE 0x6581 |
| 103 | #define VCARD7816_STATUS_ERROR_WRONG_LENGTH 0x6700 |
| 104 | #define VCARD7816_STATUS_ERROR_CLA_NOT_SUPPORTED 0x6800 |
| 105 | #define VCARD7816_STATUS_ERROR_CHANNEL_NOT_SUPPORTED 0x6881 |
| 106 | #define VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED 0x6882 |
| 107 | #define VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED 0x6900 |
| 108 | #define VCARD7816_STATUS_ERROR_COMMAND_INCOMPATIBLE_WITH_FILE 0x6981 |
| 109 | #define VCARD7816_STATUS_ERROR_SECURITY_NOT_SATISFIED 0x6982 |
| 110 | #define VCARD7816_STATUS_ERROR_AUTHENTICATION_BLOCKED 0x6983 |
| 111 | #define VCARD7816_STATUS_ERROR_DATA_INVALID 0x6984 |
| 112 | #define VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED 0x6985 |
| 113 | #define VCARD7816_STATUS_ERROR_DATA_NO_EF 0x6986 |
| 114 | #define VCARD7816_STATUS_ERROR_SM_OBJECT_MISSING 0x6987 |
| 115 | #define VCARD7816_STATUS_ERROR_SM_OBJECT_INCORRECT 0x6988 |
| 116 | #define VCARD7816_STATUS_ERROR_WRONG_PARAMETERS 0x6a00 |
| 117 | #define VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_IN_DATA 0x6a80 |
| 118 | #define VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED 0x6a81 |
| 119 | #define VCARD7816_STATUS_ERROR_FILE_NOT_FOUND 0x6a82 |
| 120 | #define VCARD7816_STATUS_ERROR_RECORD_NOT_FOUND 0x6a83 |
| 121 | #define VCARD7816_STATUS_ERROR_NO_SPACE_FOR_FILE 0x6a84 |
| 122 | #define VCARD7816_STATUS_ERROR_LC_TLV_INCONSISTENT 0x6a85 |
| 123 | #define VCARD7816_STATUS_ERROR_P1_P2_INCORRECT 0x6a86 |
| 124 | #define VCARD7816_STATUS_ERROR_LC_P1_P2_INCONSISTENT 0x6a87 |
| 125 | #define VCARD7816_STATUS_ERROR_DATA_NOT_FOUND 0x6a88 |
| 126 | #define VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_2 0x6b00 |
| 127 | #define VCARD7816_STATUS_ERROR_INS_CODE_INVALID 0x6d00 |
| 128 | #define VCARD7816_STATUS_ERROR_CLA_INVALID 0x6e00 |
| 129 | #define VCARD7816_STATUS_ERROR_GENERAL 0x6f00 |
| 130 | /* 7816 sw1 codes */ |
| 131 | #define VCARD7816_SW1_SUCCESS 0x90 |
| 132 | #define VCARD7816_SW1_RESPONSE_BYTES 0x61 |
| 133 | #define VCARD7816_SW1_WARNING 0x62 |
| 134 | #define VCARD7816_SW1_WARNING_CHANGE 0x63 |
| 135 | #define VCARD7816_SW1_EXC_ERROR 0x64 |
| 136 | #define VCARD7816_SW1_EXC_ERROR_CHANGE 0x65 |
| 137 | #define VCARD7816_SW1_ERROR_WRONG_LENGTH 0x67 |
| 138 | #define VCARD7816_SW1_CLA_ERROR 0x68 |
| 139 | #define VCARD7816_SW1_COMMAND_ERROR 0x69 |
| 140 | #define VCARD7816_SW1_P1_P2_ERROR 0x6a |
| 141 | #define VCARD7816_SW1_LE_ERROR 0x6c |
| 142 | #define VCARD7816_SW1_INS_ERROR 0x6d |
| 143 | #define VCARD7816_SW1_CLA_NOT_SUPPORTED 0x6e |
| 144 | |
| 145 | /* 7816 Instructions */ |
| 146 | #define VCARD7816_INS_MANAGE_CHANNEL 0x70 |
| 147 | #define VCARD7816_INS_EXTERNAL_AUTHENTICATE 0x82 |
| 148 | #define VCARD7816_INS_GET_CHALLENGE 0x84 |
| 149 | #define VCARD7816_INS_INTERNAL_AUTHENTICATE 0x88 |
| 150 | #define VCARD7816_INS_ERASE_BINARY 0x0e |
| 151 | #define VCARD7816_INS_READ_BINARY 0xb0 |
| 152 | #define VCARD7816_INS_WRITE_BINARY 0xd0 |
| 153 | #define VCARD7816_INS_UPDATE_BINARY 0xd6 |
| 154 | #define VCARD7816_INS_READ_RECORD 0xb2 |
| 155 | #define VCARD7816_INS_WRITE_RECORD 0xd2 |
| 156 | #define VCARD7816_INS_UPDATE_RECORD 0xdc |
| 157 | #define VCARD7816_INS_APPEND_RECORD 0xe2 |
| 158 | #define VCARD7816_INS_ENVELOPE 0xc2 |
| 159 | #define VCARD7816_INS_PUT_DATA 0xda |
| 160 | #define VCARD7816_INS_GET_DATA 0xca |
| 161 | #define VCARD7816_INS_SELECT_FILE 0xa4 |
| 162 | #define VCARD7816_INS_VERIFY 0x20 |
| 163 | #define VCARD7816_INS_GET_RESPONSE 0xc0 |
| 164 | |
| 165 | #endif |