Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 1 | #ifndef _OSMOCOM_SIM_H |
| 2 | #define _OSMOCOM_SIM_H |
| 3 | |
| 4 | #include <osmocom/core/msgb.h> |
| 5 | #include <osmocom/core/linuxlist.h> |
| 6 | |
| 7 | #define APDU_HDR_LEN 5 |
| 8 | |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 9 | /*! |
| 10 | * \file sim.h |
| 11 | * \brief Routines for helping with SIM (ISO/IEC 7816-4 more generally) communication. |
| 12 | */ |
| 13 | |
| 14 | /*! \brief command-response pairs cases |
| 15 | * |
| 16 | * Enumeration used to identify the APDU structure based on command-response pair case , as specified in ISO/IEC 7816-3:2006(E) §12.1. |
| 17 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 18 | enum osim_apdu_case { |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 19 | APDU_CASE_1, /*!< command header, no command data field, no response data field */ |
| 20 | APDU_CASE_2S, /*!< command header, no command data field, response data field (short) */ |
| 21 | APDU_CASE_2E, /*!< command header, no command data field, response data field (extended) */ |
| 22 | APDU_CASE_3S, /*!< command header, command data field (short), no response data field */ |
| 23 | APDU_CASE_3E, /*!< command header, command data field (extended), no response data field */ |
| 24 | APDU_CASE_4S, /*!< command header, command data field (short), response data field (short) */ |
| 25 | APDU_CASE_4E /*!< command header, command data field (extended), response data field (extended) */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 28 | /*! \brief APDU/TPDU command header |
| 29 | * |
| 30 | * This structure encode an APDU/TPDU command header, as specified in ISO/IEC 7816-3:2006(E) §12.2 and §12.3. |
| 31 | * The APDU (application layer) can be encoded as different TPDUs (transport layer), depending on the transport protocol used. |
| 32 | * The TPDU encoding by T=1 of the APDU command header is identical to the APDU. |
| 33 | * The TPDU encoding by T=0 of the APDU command header adds a Parameter 3 field, generally used instead of Lc/Le. |
| 34 | * |
| 35 | * @todo have different structures for APDU, TPDU by T=0, and TPDU by T=1. |
| 36 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 37 | struct osim_apdu_cmd_hdr { |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 38 | uint8_t cla; /*!< CLASS byte */ |
| 39 | uint8_t ins; /*!< INSTRUCTION byte */ |
| 40 | uint8_t p1; /*!< Parameter 1 byte */ |
| 41 | uint8_t p2; /*!< Parameter 2 byte */ |
| 42 | uint8_t p3; /*!< Parameter 3 byte, used for TPDU by T=0 */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 43 | } __attribute__ ((packed)); |
| 44 | |
| 45 | #define msgb_apdu_dr(__x) |
| 46 | |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 47 | /*! \brief APDU command body |
| 48 | * |
| 49 | * This structure encode a command body, as specified in ISO/IEC 7816-3:2006(E) §12.1. |
| 50 | * The data and response contents should be provided along with this structure. |
| 51 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 52 | struct osim_msgb_cb { |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 53 | enum osim_apdu_case apduc; /*!< command-response pair case, defining the encoding of Lc and Le */ |
| 54 | uint16_t lc; /*!< number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc, depending on the case */ |
| 55 | uint16_t le; /*!< maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le, depending on the case */ |
| 56 | uint16_t sw; /*!< status word, composed of SW1 and SW2 bytes */ |
Harald Welte | e8dd2bd | 2014-05-04 13:42:38 +0200 | [diff] [blame] | 57 | } __attribute__((__may_alias__)); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 58 | #define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0])) |
| 59 | /*! \brief status word from msgb->cb */ |
| 60 | #define msgb_apdu_case(__x) OSIM_MSGB_CB(__x)->apduc |
| 61 | #define msgb_apdu_lc(__x) OSIM_MSGB_CB(__x)->lc |
| 62 | #define msgb_apdu_le(__x) OSIM_MSGB_CB(__x)->le |
| 63 | #define msgb_apdu_sw(__x) OSIM_MSGB_CB(__x)->sw |
| 64 | /*! \brief pointer to the command header of the APDU */ |
| 65 | #define msgb_apdu_h(__x) ((struct osim_apdu_cmd_hdr *)(__x)->l2h) |
| 66 | |
| 67 | #define msgb_apdu_dc(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr)) |
| 68 | #define msgb_apdu_de(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr) + msgb_apdu_lc(__x)) |
| 69 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 70 | /* FILES */ |
| 71 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 72 | struct osim_file; |
| 73 | struct osim_file_desc; |
| 74 | struct osim_decoded_data; |
| 75 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 76 | /*! \brief Operations for a given File */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 77 | struct osim_file_ops { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 78 | /*! Parse binary file data into osim_decoded_data */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 79 | int (*parse)(struct osim_decoded_data *dd, |
| 80 | const struct osim_file_desc *desc, |
| 81 | int len, uint8_t *data); |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 82 | /*! Encode osim_decoded_data into binary file */ |
Harald Welte | 6729a97 | 2014-10-26 19:04:56 +0100 | [diff] [blame] | 83 | struct msgb * (*encode)(const struct osim_file_desc *desc, |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 84 | const struct osim_decoded_data *decoded); |
| 85 | }; |
| 86 | |
| 87 | enum osim_element_type { |
| 88 | ELEM_T_NONE, |
| 89 | ELEM_T_BOOL, /*!< a boolean flag */ |
| 90 | ELEM_T_UINT8, /*!< unsigned integer */ |
| 91 | ELEM_T_UINT16, /*!< unsigned integer */ |
| 92 | ELEM_T_UINT32, /*!< unsigned integer */ |
| 93 | ELEM_T_STRING, /*!< generic string */ |
| 94 | ELEM_T_BCD, /*!< BCD encoded digits */ |
| 95 | ELEM_T_BYTES, /*!< BCD encoded digits */ |
| 96 | ELEM_T_GROUP, /*!< group container, has siblings */ |
| 97 | }; |
| 98 | |
| 99 | enum osim_element_repr { |
| 100 | ELEM_REPR_NONE, |
| 101 | ELEM_REPR_DEC, |
| 102 | ELEM_REPR_HEX, |
| 103 | }; |
| 104 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 105 | /*! \brief A single decoded element inside a file */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 106 | struct osim_decoded_element { |
| 107 | struct llist_head list; |
| 108 | |
| 109 | enum osim_element_type type; |
| 110 | enum osim_element_repr representation; |
| 111 | const char *name; |
| 112 | |
| 113 | unsigned int length; |
| 114 | union { |
| 115 | uint8_t u8; |
| 116 | uint16_t u16; |
| 117 | uint32_t u32; |
| 118 | uint8_t *buf; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 119 | /*! A list of sibling decoded_items */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 120 | struct llist_head siblings; |
| 121 | } u; |
| 122 | }; |
| 123 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 124 | /*! Decoded data for a single file, consisting of all decoded elements */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 125 | struct osim_decoded_data { |
| 126 | /*! file to which we belong */ |
| 127 | const struct osim_file *file; |
| 128 | /*! list of 'struct decoded_element' */ |
| 129 | struct llist_head decoded_elements; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | enum osim_file_type { |
| 134 | TYPE_NONE, |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 135 | TYPE_DF, /*!< Dedicated File */ |
| 136 | TYPE_ADF, /*!< Application Dedicated File */ |
| 137 | TYPE_EF, /*!< Entry File */ |
| 138 | TYPE_EF_INT, /*!< Internal Entry File */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | enum osim_ef_type { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 142 | EF_TYPE_TRANSP, /*!< Transparent EF */ |
| 143 | EF_TYPE_RECORD_FIXED, /*!< Fixed-Size Record EF */ |
| 144 | EF_TYPE_RECORD_CYCLIC, /*!< Cyclic Record EF */ |
| 145 | EF_TYPE_KEY, /*!< Key file as used in TETRA */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | #define F_OPTIONAL 0x0001 |
| 149 | |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 150 | #define SFI_NONE 0xFF |
| 151 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 152 | struct osim_file_desc { |
| 153 | struct llist_head list; /*!< local element in list */ |
| 154 | struct llist_head child_list; /*!< list of children EF in DF */ |
| 155 | struct osim_file_desc *parent; /*!< parent DF */ |
| 156 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 157 | enum osim_file_type type; /*!< Type of the file (EF, DF, ...) */ |
| 158 | enum osim_ef_type ef_type; /*!< Type of the EF, if type == TYPE_EF */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 159 | |
| 160 | uint16_t fid; /*!< File Identifier */ |
| 161 | uint8_t sfid; /*!< Short File IDentifier */ |
Harald Welte | 6729a97 | 2014-10-26 19:04:56 +0100 | [diff] [blame] | 162 | const uint8_t *df_name; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 163 | uint8_t df_name_len; |
| 164 | |
| 165 | const char *short_name; /*!< Short Name (like EF.ICCID) */ |
| 166 | const char *long_name; /*!< Long / description */ |
| 167 | unsigned int flags; |
| 168 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 169 | struct osim_file_ops ops; /*!< Operations (parse/encode */ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 170 | |
| 171 | struct { |
| 172 | size_t min; /*!< Minimum size of the file |
| 173 | (transparent) or record in |
| 174 | cyclic / linear file */ |
| 175 | size_t rec; /*!< Recommended size */ |
| 176 | } size; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 177 | }; |
| 178 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 179 | /*! \brief A single instance of a file: Descriptor and contents */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 180 | struct osim_file { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 181 | /*! Descriptor for the file */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 182 | const struct osim_file_desc *desc; |
| 183 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 184 | /*! Encoded file contents */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 185 | struct msgb *encoded_data; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 186 | /*! Parsed/Decoded file contents */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 187 | struct osim_decoded_data *decoded_data; |
| 188 | }; |
| 189 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 190 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 191 | #define EF(pfid, sfi, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 192 | { \ |
| 193 | .fid = pfid, \ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 194 | .sfid = sfi, \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 195 | .type = TYPE_EF, \ |
| 196 | .ef_type = ptype, \ |
| 197 | .short_name = pns, \ |
| 198 | .long_name = pnl, \ |
| 199 | .flags = pflags, \ |
| 200 | .ops = { .encode = penc, .parse = pdec }, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 201 | .size = { .min = smin, .rec = srec}, \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 205 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 206 | #define EF_TRANSP(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 207 | EF(fid, sfi, ns, flags, nl, EF_TYPE_TRANSP, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 208 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 209 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 210 | #define EF_TRANSP_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 211 | EF_TRANSP(fid, sfi, ns, flags, smin, srec, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 212 | nl, &default_decode, NULL) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 213 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 214 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 215 | #define EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 216 | EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 217 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 218 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 219 | #define EF_CYCLIC_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 220 | EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 221 | &default_decode, NULL) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 222 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 223 | /*! Convenience macros for defining EF */ |
Harald Welte | 2656e65 | 2014-05-03 14:23:44 +0200 | [diff] [blame] | 224 | #define EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 225 | EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_FIXED, \ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 226 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 227 | /*! Convenience macros for defining EF */ |
Harald Welte | 1e0dfda | 2014-05-03 14:22:12 +0200 | [diff] [blame] | 228 | #define EF_LIN_FIX_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 229 | EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \ |
| 230 | &default_decode, NULL) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 231 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 232 | /*! Convenience macros for defining EF */ |
Harald Welte | aad7e06 | 2014-05-04 16:31:24 +0200 | [diff] [blame] | 233 | #define EF_KEY(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ |
| 234 | EF(fid, sfi, ns, flags, nl, EF_TYPE_KEY, \ |
| 235 | smin, srec, dec, enc) |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 236 | /*! Convenience macros for defining EF */ |
Harald Welte | aad7e06 | 2014-05-04 16:31:24 +0200 | [diff] [blame] | 237 | #define EF_KEY_N(fid, sfi, ns, flags, smin, srec, nl) \ |
| 238 | EF_KEY(fid, sfi, ns, flags, smin, srec, nl, \ |
| 239 | &default_decode, NULL) |
| 240 | |
| 241 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 242 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 243 | osim_file_desc_find_name(struct osim_file_desc *parent, const char *name); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 244 | |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 245 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 246 | osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid); |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 247 | |
| 248 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 249 | osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid); |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 250 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 251 | /* STATUS WORDS */ |
| 252 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 253 | enum osim_card_sw_type { |
| 254 | SW_TYPE_NONE, |
| 255 | SW_TYPE_STR, |
| 256 | }; |
| 257 | |
| 258 | enum osim_card_sw_class { |
| 259 | SW_CLS_NONE, |
| 260 | SW_CLS_OK, |
| 261 | SW_CLS_POSTP, |
| 262 | SW_CLS_WARN, |
| 263 | SW_CLS_ERROR, |
| 264 | }; |
| 265 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 266 | /*! A card status word (SW) */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 267 | struct osim_card_sw { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 268 | /*! status word code (2 bytes) */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 269 | uint16_t code; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 270 | /*! status word mask (2 bytes), to match range/prefix of SW */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 271 | uint16_t mask; |
| 272 | enum osim_card_sw_type type; |
| 273 | enum osim_card_sw_class class; |
| 274 | union { |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 275 | /*! Human-readable meaning of SW */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 276 | const char *str; |
| 277 | } u; |
| 278 | }; |
| 279 | |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 280 | #define OSIM_CARD_SW_LAST (const struct osim_card_sw) { \ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 281 | .code = 0, .mask = 0, .type = SW_TYPE_NONE, \ |
| 282 | .class = SW_CLS_NONE, .u.str = NULL \ |
| 283 | } |
| 284 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 285 | /*! \brief A card profile (e.g. SIM card */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 286 | struct osim_card_profile { |
| 287 | const char *name; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 288 | /*! Descriptor for the MF (root directory */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 289 | struct osim_file_desc *mf; |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 290 | /*! Array of pointers to status words */ |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 291 | const struct osim_card_sw **sws; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 292 | }; |
| 293 | |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 294 | const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp, |
| 295 | uint16_t sw); |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 296 | enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp, |
| 297 | uint16_t sw_in); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 298 | |
| 299 | struct osim_card_hdl; |
| 300 | char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in); |
| 301 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 302 | extern const struct tlv_definition ts102221_fcp_tlv_def; |
Holger Hans Peter Freyther | c2b4458 | 2015-04-11 19:31:03 +0200 | [diff] [blame] | 303 | extern const struct value_string ts102221_fcp_vals[14]; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 304 | |
| 305 | /* 11.1.1.3 */ |
| 306 | enum ts102221_fcp_tag { |
| 307 | UICC_FCP_T_FCP = 0x62, |
| 308 | UICC_FCP_T_FILE_SIZE = 0x80, |
| 309 | UICC_FCP_T_TOT_F_SIZE = 0x81, |
| 310 | UICC_FCP_T_FILE_DESC = 0x82, |
| 311 | UICC_FCP_T_FILE_ID = 0x83, |
| 312 | UICC_FCP_T_DF_NAME = 0x84, |
| 313 | UICC_FCP_T_SFID = 0x88, |
| 314 | UICC_FCP_T_LIFEC_STS = 0x8A, |
| 315 | UICC_FCP_T_SEC_ATTR_REFEXP= 0x8B, |
| 316 | UICC_FCP_T_SEC_ATTR_COMP= 0x8C, |
| 317 | UICC_FCP_T_PROPRIETARY = 0xA5, |
| 318 | UICC_FCP_T_SEC_ATTR_EXP = 0xAB, |
| 319 | UICC_FCP_T_PIN_STS_DO = 0xC6, |
| 320 | }; |
| 321 | |
| 322 | struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, |
| 323 | uint8_t p2, uint16_t lc, uint16_t le); |
| 324 | |
Harald Welte | 586d710 | 2014-05-04 17:01:16 +0200 | [diff] [blame] | 325 | /* CARD READERS */ |
| 326 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 327 | enum osim_proto { |
| 328 | OSIM_PROTO_T0 = 0, |
| 329 | OSIM_PROTO_T1 = 1, |
| 330 | }; |
| 331 | |
| 332 | enum osim_reader_driver { |
| 333 | OSIM_READER_DRV_PCSC = 0, |
| 334 | OSIM_READER_DRV_OPENCT = 1, |
| 335 | OSIM_READER_DRV_SERIAL = 2, |
| 336 | }; |
| 337 | |
Harald Welte | 932d2d4 | 2016-03-19 21:18:40 +0100 | [diff] [blame] | 338 | struct osim_reader_ops { |
| 339 | const char *name; |
| 340 | struct osim_reader_hdl *(*reader_open)(int idx, const char *name, void *ctx); |
| 341 | struct osim_card_hdl *(*card_open)(struct osim_reader_hdl *rh, enum osim_proto proto); |
| 342 | int (*transceive)(struct osim_reader_hdl *rh, struct msgb *msg); |
| 343 | }; |
| 344 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 345 | struct osim_reader_hdl { |
| 346 | /*! \brief member in global list of readers */ |
| 347 | struct llist_head list; |
Harald Welte | 67354b1 | 2014-10-26 18:47:30 +0100 | [diff] [blame] | 348 | const struct osim_reader_ops *ops; |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 349 | uint32_t proto_supported; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 350 | void *priv; |
| 351 | /*! \brief current card, if any */ |
| 352 | struct osim_card_hdl *card; |
| 353 | }; |
| 354 | |
| 355 | struct osim_card_hdl { |
| 356 | /*! \brief member in global list of cards */ |
| 357 | struct llist_head list; |
| 358 | /*! \brief reader through which card is accessed */ |
| 359 | struct osim_reader_hdl *reader; |
| 360 | /*! \brief card profile */ |
| 361 | struct osim_card_profile *prof; |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 362 | /*! \brief card protocol */ |
| 363 | enum osim_proto proto; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 364 | |
| 365 | /*! \brief list of channels for this card */ |
| 366 | struct llist_head channels; |
| 367 | }; |
| 368 | |
| 369 | struct osim_chan_hdl { |
| 370 | /*! \brief linked to card->channels */ |
| 371 | struct llist_head list; |
| 372 | /*! \brief card to which this channel belongs */ |
| 373 | struct osim_card_hdl *card; |
| 374 | const struct osim_file_desc *cwd; |
| 375 | }; |
| 376 | |
| 377 | /* reader.c */ |
| 378 | int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg); |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 379 | struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver drv, int idx, |
| 380 | const char *name, void *ctx); |
| 381 | struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 382 | #endif /* _OSMOCOM_SIM_H */ |